var rowSep = String.fromCharCode(2, 4), cellSep = String.fromCharCode(3, 4), roSep = "#";
var returnValue = null, alternativeAlertString = null, newLineArray = null, currentHGridElem = null, currentHGridSel = null,
	objToElement = null, arrayReadOnlySource = null, objForScript = null;
var colorWhite = "#FFFFFF", colorGrey = "#DEDEDE";
var arrayGridId = new Array(), arrayGridSource = new Array(), arrayGridChanges = new Array();
var bGenerateEmptyGrid = false, bAlternativeDeleteString = true;

function SwitchStringToArray(objParam)
{
	var i = 0;
	var a = objParam.split(rowSep);
	var nLength = a.length;
	var objResult = (objParam=="")? new Array(): new Array(nLength);

	while(i<nLength && objParam!="")
		objResult[i] = a[i++].toString().split(cellSep);

	return objResult;
}

function GetROValues(strFormatted)
{
	return strFormatted.split(roSep);
}

function GenerateGrid(gridId, bReload)
{
var arrIds = arrayGridId;
var arrMatrix = arrayGridSource;
var arrNewValues = arrayGridChanges;

	SelectControlHeaders(null);
	currentHGridSel = null;
	var gridOuter = GetGridSpan(gridId);
	if (gridOuter == null)
		return;
	var inpReadOnly = document.all(gridId + "ReadOnlyLines");
	var oGrid = gridOuter.childNodes(1).childNodes(0);
	var templateRowCol = oGrid.rows(oGrid.rows.length-1).childNodes;
	var gridRO = new Array();
	var gridValues = new Array();
	var trElement, tdElement, tdCollection;
	var inpStatus = document.all(gridId + "ChangeFlag");
	var strGridID = document.all(gridId).value;
	if (!bReload)
	{
		gridValues = SwitchStringToArray(strGridID);
		arrIds[arrIds.length] = gridId;
		arrMatrix[arrMatrix.length] = gridValues;
		arrNewValues[arrNewValues.length] = new Array();
		inpStatus.index = arrIds.length - 1;
	}
	else
		gridValues = arrMatrix[IntFrom(inpStatus.index)];

	if (inpReadOnly.value != "")
	{
		if (arrayReadOnlySource == null)
			gridRO = GetROValues(inpReadOnly.value);
		else
			gridRO = arrayReadOnlySource;
	}
	var objGridChildNodes = oGrid.childNodes;
	for(var i=0; i<objGridChildNodes(0).childNodes.length;)
		objGridChildNodes(0).removeChild(objGridChildNodes(0).childNodes(0));

	if (!bGenerateEmptyGrid)
	{
		var nFirst = 0, nLast = gridValues.length;

		if (document.all(gridId + "Paging") != null)
		{
			var nPageSize = IntFrom(document.all(gridId + "Paging").PageSize);
			var nPageIndex = IntFrom(document.all(gridId + "Paging").PageIndex);
			var mPageNo = Math.ceil(gridValues.length/nPageSize);

			if (mPageNo == 0)
				++mPageNo;

			if (nPageIndex >= mPageNo)
			{
				nPageIndex = mPageNo - 1;
				document.all(gridId + "Paging").PageIndex = nPageIndex;
			}

			if (nPageIndex == 0)
			{
				nFirst = 0;
				nLast = (nPageSize<=gridValues.length)? nPageSize: gridValues.length;
			}
			else
			{
				nFirst = nPageSize * nPageIndex;
				nLast = (nPageSize*(nPageIndex+1)<gridValues.length)? nPageSize*(nPageIndex+1) : gridValues.length;
			}
			document.all(gridId + "CPageNo").value = nPageIndex + 1;
			document.all(gridId + "CPageNo").InitialValue = nPageIndex;
			document.all(gridId + "MPageNo").innerText = mPageNo;
			document.all(gridId + "CPageSize").InitialValue = nPageSize;
			document.all(gridId + "CPageSize").value = nPageSize;
		}

		for (var i=nFirst; i<nLast; ++i)
		{
			trElement = oGrid.insertRow();
			tdElement = trElement.insertCell();
			tdElement.innerHTML = templateRowCol(0).innerHTML;
			tdCollection = tdElement.childNodes;
			tdCollection(0).value = i + 1;
			tdElement.style.backgroundColor = tdCollection(0).style.backgroundColor;
			tdCollection(0).LineAdded = "true";

			if (gridRO.length > 0)
				FormatLineHeader(tdCollection, !(gridRO[i] == "1" || gridRO[i].toString().toUpperCase() == "TRUE" || gridOuter.GridReadOnly || gridOuter.DeleteLine == "false"))
			else
				FormatLineHeader(tdCollection, !(gridOuter.GridReadOnly || gridOuter.DeleteLine == "false"))

			for (var j=1; j<templateRowCol.length; ++j)
			{
				tdElement = trElement.insertCell();
				tdElement.innerHTML = templateRowCol(j).innerHTML;
				var ctrlElem = tdElement.childNodes(0);
				if (IntFrom(ctrlElem.style.width) == 0)
					tdElement.style.border = "0";

				if(ctrlElem.ControlType == "HDate")
				{
					SetHDateValueDateTime(ctrlElem.childNodes(0), gridValues[i][j-1]);
					if (ctrlElem.ControlReadOnly != true && ctrlElem.ControlReadOnly != "true")
					{
						if(gridRO.length > 0)
							SetHControlReadOnly(ctrlElem, gridRO[i] == "1" || gridRO[i].toString().toUpperCase() == "TRUE" || gridOuter.GridReadOnly);
						else
							SetHControlReadOnly(ctrlElem, gridOuter.GridReadOnly);
					}
				}
				if(ctrlElem.ControlType == "HInput")
				{
					ctrlElem.value = ShortInitialFormat(ctrlElem,gridValues[i][j-1]);
					ctrlElem.title = (ctrlElem.ToolTip)?ctrlElem.value:null;
					ctrlElem.InitialValue = gridValues[i][j-1];
					if (ctrlElem.ControlReadOnly != true && ctrlElem.ControlReadOnly != "true")
					{
						if(gridRO.length > 0)
							SetHControlReadOnly(ctrlElem, gridRO[i] == "1" || gridRO[i].toString().toUpperCase() == "TRUE" || gridOuter.GridReadOnly);
						else
//								SetHControlReadOnly(ctrlElem, gridOuter.GridReadOnly || gridOuter.DeleteLine == "false");
							SetHControlReadOnly(ctrlElem, gridOuter.GridReadOnly);
					}
				}
				if(ctrlElem.ControlType == "HSelect")
				{
					var ctrlSelect = ctrlElem.parentElement.childNodes(1);
					for (var k=0; k<ctrlSelect.options.length; ++k)
						if ((k == 0 || ctrlSelect.options[k].value != "") && ctrlSelect.options[k].value == gridValues[i][j-1])
						{
							ctrlElem.value = ctrlSelect.options[k].text;
							ctrlElem.title = (ctrlElem.ToolTip)?ctrlSelect.options[k].text:null;
							ctrlElem.CodeValue = ctrlSelect.options[k].value;
							ctrlSelect.options[k].selected = true;
							ctrlElem.InitialSelectedIndex = k;
						}
					if (ctrlElem.ControlReadOnly != true && ctrlElem.ControlReadOnly != "true")
					{
						if(gridRO.length > 0)
							SetHControlReadOnly(ctrlElem, gridRO[i] == "1" || gridRO[i].toString().toUpperCase() == "TRUE" || gridOuter.GridReadOnly);
						else
							SetHControlReadOnly(ctrlElem, gridOuter.GridReadOnly);
					}
				}
				if(ctrlElem.ControlType == "HSelection")
				{
					ctrlElem.value = gridValues[i][j-1];
					ctrlElem.InitialValue = gridValues[i][j-1];
					ctrlElem.title = (ctrlElem.ToolTip)?gridValues[i][j-1]:null;
					if ((ctrlElem.ControlPartialReadOnly == true || ctrlElem.ControlPartialReadOnly == "true") && 
						(ctrlElem.ControlReadOnly != true && ctrlElem.ControlReadOnly != "true") && 
						(gridRO.length == 0))
						SetHSelectionPartialReadOnly(ctrlElem, true, colorWhite);
					else
						if (ctrlElem.ControlReadOnly != true && ctrlElem.ControlReadOnly != "true")
						{
							if(gridRO.length > 0)
								SetHControlReadOnly(ctrlElem, gridRO[i] == "1" || gridRO[i].toString().toUpperCase() == "TRUE" || gridOuter.GridReadOnly);
							else
								SetHControlReadOnly(ctrlElem, gridOuter.GridReadOnly);
						}
				}
				if(ctrlElem.ControlType == "HCheckBox")
				{
					ctrlElem.checked = gridValues[i][j-1] != "" && gridValues[i][j-1].toString().toUpperCase() == "TRUE";
					if (ctrlElem.ControlReadOnly != true && ctrlElem.ControlReadOnly != "true")
					{
						if(gridRO.length > 0)
							SetHControlReadOnly(ctrlElem, gridRO[i] == "1" || gridRO[i].toString().toUpperCase() == "TRUE" || gridOuter.GridReadOnly);
						else
							SetHControlReadOnly(ctrlElem, gridOuter.GridReadOnly);
					}
				}
			}
		}
	}
	
	trElement = oGrid.insertRow();
	for (var j=0; j<templateRowCol.length; ++j)
	{
		tdElement = trElement.insertCell();
		tdElement.innerHTML = templateRowCol(j).innerHTML;
		tdElement.childNodes(0).LineAdded = "false";
		if (j == 0)
			FormatLineHeader(tdElement.childNodes, false);
		else
		{
			var ctrlElem = tdElement.childNodes(0);
			if (IntFrom(ctrlElem.style.width) == 0)
				tdElement.style.border = "0";

			if(ctrlElem.ControlType == "HDate")
				SetHDateValueDateTime(ctrlElem.childNodes(0), (ctrlElem.DefaultValue != null)?ctrlElem.DefaultValue:"");
			if(ctrlElem.ControlType == "HInput")
			{
				if (ctrlElem.DefaultValue != null)
				{
					ctrlElem.value = ShortInitialFormat(ctrlElem, ctrlElem.DefaultValue);
					ctrlElem.title = (ctrlElem.ToolTip)?ctrlElem.value:null;
					ctrlElem.InitialValue = ctrlElem.DefaultValue;
				}
				else
					ctrlElem.value = ctrlElem.InitialValue = "";
			}
			if(ctrlElem.ControlType == "HSelect")
			{
				if (ctrlElem.DefaultValue != null)
					SetHControlValue(ctrlElem, ctrlElem.DefaultValue, false, false);
				else
				{
					ctrlElem.value = ctrlElem.CodeValue = "";
					var ctrlSelect = ctrlElem.parentElement.childNodes(1);
					if (ctrlSelect.options.length > 0 && ctrlSelect.selectedIndex >= 0)
						ctrlSelect.options[ctrlSelect.selectedIndex].selected = false;
				}
			}
			if(ctrlElem.ControlType == "HSelection")
			{
				SetHControlValue(ctrlElem, (ctrlElem.DefaultValue != null)?ctrlElem.DefaultValue:"", false, false);
				SetHSelectionPartialReadOnly(ctrlElem, ctrlElem.ControlPartialReadOnly, colorWhite);
			}
			if(ctrlElem.ControlType == "HCheckBox")
				ctrlElem.checked = ctrlElem.DefaultValue;
		}
	}

	var bFooterVisible = true;
	if (gridOuter.GridReadOnly || gridOuter.MaxRowsCount != null && gridValues.length >= IntFrom(gridOuter.MaxRowsCount) ||
		document.all(gridId + "ShowFooter").value.toUpperCase() == "FALSE" || nLast != gridValues.length)
			bFooterVisible = false;

	gridOuter.style.display = "";
	SetGridFooterVisible(gridId, bFooterVisible);
	arrayReadOnlySource = null;
	ResizeGrid(oGrid);
}

function FormatLineHeader(objElem, bNotEdit)
{
	if (bNotEdit)
	{
		objElem(0).style.width = 25;
		objElem(1).style.display = "";
	}
	else
	{
		objElem(0).ReadOnlyLine = "false";
		objElem(0).style.width = 35;
		objElem(0).parentElement.style.backgroundColor = objElem(0).style.backgroundColor;
		objElem(1).style.display = "none";
	}
}

function DeleteAllLines(tempObj, strMsg)
{
var arrMatrix = arrayGridSource;

	var gridId = tempObj.GridId;
	
	var oGrid = GetGridObject(gridId);
	
	if (oGrid.rows.length < 2)
		return;

	var bDelete = true;
	if (bAlternativeDeleteString)
		bDelete = window.confirm(strMsg);

	if (bDelete)
	{
		if (document.all(gridId + "InvokeOnDeleteAllLines") != null && !DoCallExecScript(document.all(gridId + "InvokeOnDeleteAllLines").value))
			return false;

		var xParams = new Array(gridId);
		var xResult = DoCallWebService_Grid("DeleteAll_NEW", xParams);
		if (xResult != null)
		{
			var inpStatus = document.all(gridId + "ChangeFlag");
			inpStatus.value = "unchanged";
			var iPos = IntFrom(inpStatus.index);
			var gridValues = new Array();
			gridValues = arrMatrix[iPos];
			gridValues.splice(0,gridValues.length);
			GetGridSpan(gridId).childNodes(0).value = "";
			var oGrid = GetGridObject(gridId);
			var iLen = oGrid.rows.length;

			for (var i=0; i<iLen-1; ++i)
				oGrid.rows(0).removeNode(true);

			var gridOuter = GetGridSpan(gridId);
			if (gridOuter.MaxRowsCount != null && oGrid.rows.length <= IntFrom(gridOuter.MaxRowsCount))
				oGrid.rows(oGrid.rows.length - 1).style.display = "";

			if (document.all(gridId + "Paging") != null)
			{
				document.all(gridId + "Paging").PageIndex = 0;
				document.all(gridId + "CPageNo").value = 1;
				document.all(gridId + "CPageNo").InitialValue = 0;
				document.all(gridId + "MPageNo").innerText = 1;
				SetGridFooterVisible(gridId, true);
			}

			ResizeGrid(oGrid);
		}
	}
}

function DoCallExecScript(strScript, oTmp, bEvent, bFocus, bInput)
{
	returnValue = null;
	if(oTmp != null && oTmp.ControlType == "HSelection")
		objToElement = null;
	alternativeAlertString = null;
	window.execScript(strScript, "JavaScript");
	if(bInput != true)
		bInput = false;
	if (returnValue != null && !returnValue && (!bInput || event.toElement != oTmp.parentElement.parentElement.cells(0).childNodes(1)))
	{
//		if(oTmp.ControlType == "HSelection" && oTmp.parentElement.childNodes(1).style.display != "")
//			oTmp.style.width = IntFrom(oTmp.style.width) - IntFrom(oTmp.parentElement.childNodes(1).style.width) - 1;
		returnValue = null;
		if (alternativeAlertString != null)
			alert(alternativeAlertString);
		else
			if (oTmp != null && oTmp.AlertInvalidString != null)
				alert(oTmp.AlertInvalidString);
		if(bEvent)
			window.event.returnValue = false;
		if(bFocus && oTmp.ControlType != "HSelection")
			oTmp.focus();
		if(bFocus && oTmp.ControlType == "HSelection" && objToElement != null)
			objToElement.focus();
		return false;
	}
	return true;
}

function DoCallExecScriptAndInvalidate(strScript, oTmp)
{
	alert(strScript);
	window.event.returnValue = false;
	oTmp.focus();
	return false;
}

function OnHInputFocus(oTmp)
{
	if (oTmp.GridControl && document.all(oTmp.GridId + "InvokeOnChangeLine") != null)
		DoCallExecScript(document.all(oTmp.GridId + "InvokeOnChangeLine").value);

	if (oTmp.InvokeOnActivate != null)
		DoCallExecScript(oTmp.InvokeOnActivate, oTmp, true);

	if (oTmp.GridControl)
	{
		SelectControlHeaders(oTmp);
		/*if(typeof(OnSelect_BudgetDefinition) != 'undefined')
			OnSelect_BudgetDefinition();*/
	}
	if (IsCtrRO(oTmp))
		return;
	else
	{
		if (oTmp.Formatted)
		{
			oTmp.InitialValue = oTmp.value = UnformatHInput(oTmp.value,oTmp.Sufix,oTmp.Prefix,oTmp.Comma,oTmp.Decimal,false);
			oTmp.Formatted = !oTmp.Formatted;
		}
	}
	
	SetFocusOnElem(oTmp);
}

function SetFocusOnElem(ctrlElem, bHSel)
{
	if (ctrlElem.GridControl)
	{
		ctrlElem.parentElement.style.border = "black 1px solid";
		ctrlElem.parentElement.style.backgroundColor = "black";
		ctrlElem.style.border = "black 1px solid";
	}
	switch(ctrlElem.ControlType)
	{
		case "HInput":
			currentHGridElem = ctrlElem;
			window.setTimeout("currentHGridElem.select()", 10);
			break;
		case "HSelect":
			ctrlElem.style.display = "none";
			var ctrlSelect = ctrlElem.parentElement.childNodes(1);
			ctrlSelect.style.display = "";
			try
			{
				if (ctrlSelect.options.length > 0 && ctrlSelect.getAttribute("FirstTime") && ctrlSelect.options[ctrlSelect.selectedIndex].value != "")
				{
					if (ctrlElem.value == "")
						ctrlSelect.fireEvent("onchange");
					ctrlSelect.setAttribute("FirstTime", false);
				}
			}catch(exception){}
			ctrlSelect.setActive();
			currentHGridElem = ctrlElem.parentElement.childNodes(1);
			break;
		case "HSelection":
			if(bHSel)
			{
				if (ctrlElem.parentElement.childNodes(1).style.display != "")
				{
					ctrlElem.style.width = IntFrom(ctrlElem.style.width) - IntFrom(ctrlElem.parentElement.childNodes(1).style.width) - 1;
					ctrlElem.parentElement.childNodes(1).style.display = "";
				}
			}
			ctrlElem.style.borderRight = "";
			if(bHSel)
			{
				currentHGridElem = ctrlElem;
				window.setTimeout("currentHGridElem.select()", 10);
			}
			break;
		case "HCheckBox":
			ctrlElem.style.width = ctrlElem.offsetWidth;
			currentHGridElem = ctrlElem;
			break;
	}
}

function SetBlurOnElem(ctrlElem, bHSel)
{
	var strControlType = ctrlElem.ControlType;
	if(strControlType == null)
		strControlType = "HSelect"
	ctrlElem.parentElement.style.border = "gray 1px solid";
	ctrlElem.parentElement.style.backgroundColor = colorWhite;
	ctrlElem.style.border = "";

	switch(strControlType)
	{
		case "HSelect":
			ctrlElem.parentElement.childNodes(0).style.border = "";	
			ctrlElem.parentElement.childNodes(0).style.display = "";
			break;
		case "HSelection":
			ctrlElem.style.borderRight = "";
			if (bHSel)
			{
//				if(IntFrom(ctrlElem.style.width) + IntFrom(ctrlElem.parentElement.childNodes(1).style.width)<IntFrom(ctrlElem.parentElement.offsetWidth))
				if (ctrlElem.parentElement.childNodes(1).style.display != "none")
				{
					ctrlElem.style.width = IntFrom(ctrlElem.style.width) + IntFrom(ctrlElem.parentElement.childNodes(1).style.width) + 1;
					ctrlElem.parentElement.childNodes(1).style.display = "none";
				}
			}
			break;
	}
}

function OnHInputBlur(oTmp)
{
	try
	{
		if (FocusIsInGrid(oTmp))
			return;

		if (oTmp.InvokeOnBeforeDeactivate != null && !DoCallExecScript(oTmp.InvokeOnBeforeDeactivate, oTmp, true, true))
			return false;

		var bChange = false;
		var bFormatted = oTmp.Formatted;
		if (oTmp.value != oTmp.InitialValue)
		{
			if (ValidateHInput(oTmp))
			{
				if (!bFormatted)
				{
					oTmp.InitialValue = oTmp.value = FormatHInput(oTmp.value,oTmp.Sufix,oTmp.Prefix,oTmp.Comma,oTmp.Decimal,oTmp.nDecimals,oTmp.NumericFieldType);
					oTmp.title = (oTmp.ToolTip != null)?oTmp.value:null;
					oTmp.Formatted = bChange = true;
				}
			}
			else
				return;
		}
		else
		{
			if (!bFormatted)
			{
				oTmp.InitialValue = oTmp.value = FormatHInput(oTmp.value,oTmp.Sufix,oTmp.Prefix,oTmp.Comma,oTmp.Decimal,oTmp.nDecimals,oTmp.NumericFieldType);
				oTmp.Formatted = true;
			}
		}
		if (oTmp.GridControl)
		{
			var trElement = oTmp.parentElement.parentElement;
			if (trElement.rowIndex == trElement.parentElement.childNodes.length - 2 && !ValidateLine(oTmp))
				return;

			if (bChange && VerifyLastLine(trElement))
				document.all(oTmp.GridId + "ChangeFlag").value = "changed";
				
			SetBlurOnElem(oTmp);
		}
	}
	catch(e){}
}

function VerifyLastLine(oTmp)
{
	return oTmp.parentElement != null && (oTmp.rowIndex != oTmp.parentElement.rows.length - 2 || (oTmp.rowIndex == oTmp.parentElement.rows.length - 2 && oTmp.childNodes(0).childNodes(0).LineAdded == "true"));
}

function OnHInputKeyUp(oTmp)
{
	if (oTmp.value == "" || oTmp.DefaultValue != null && oTmp.value == oTmp.DefaultValue)
		return;
	if (oTmp.GridControl)
	{
		var trElement = oTmp.parentElement.parentElement;
		if (trElement.rowIndex != trElement.parentElement.rows.length - 1)
			return;
		InsertBlankRow(GetGridObject(oTmp.GridId));
	}
}

function ShortInitialFormat(oTmp, strValue)
{
	if(oTmp.Required || strValue != "")
		return InitialFormat(strValue,oTmp.Sufix,oTmp.Prefix,oTmp.Comma,oTmp.Decimal,oTmp.nDecimals,oTmp.NumericFieldType);
	else
		return "";
}

function FormatNewLineControl(tempElem)
{
	if(tempElem.ControlType == "HDate")
	{
		tempElem.childNodes(0).style.display = "";
		tempElem.childNodes(1).style.display = "none";
		tempElem.style.backgroundColor = tempElem.Background;
	}
	else
		tempElem.backgroundColor = colorWhite;
	if (IntFrom(tempElem.style.width) != 0)
		tempElem.parentElement.style.border = "gray 1px solid";

	if(tempElem.ControlType == "HDate")
	{
		tempElem.parentElement.style.backgroundColor = "white";
		tempElem.childNodes(1).style.backgroundColor = "transparent";
	}
	else
		tempElem.style.border = "";
}

function InsertBlankRow(oGrid)
{
var arrMatrix = arrayGridSource;

	var trHeader = oGrid.parentElement.parentElement.childNodes(0).childNodes(0).rows(0);
	for (var i=2; i<trHeader.cells.length; ++i)
	{
		if (trHeader.cells(i).childNodes(0).childNodes.length == 2 && trHeader.cells(i).childNodes(0).childNodes(1).style.display == "")
		{
			trHeader.cells(i).childNodes(0).childNodes(1).style.display = "none";
			i = trHeader.cells.length;
		}
	}
	var trFooter = oGrid.rows(oGrid.rows.length-1);
	var tdElement, trElement = oGrid.insertRow();
	for (var i=0; i<trFooter.childNodes.length; ++i)
	{
		tdElement = trElement.insertCell();
		tdElement.innerHTML = trFooter.childNodes(i).innerHTML;
		var oTmp = tdElement.childNodes(0);

		if (i == 0)
		{
			var editHeaderColor;

			editHeaderColor = document.all(oTmp.GridId + "HRBGColor").value;
			tdElement.style.backgroundColor = editHeaderColor;
			tdElement.childNodes(0).style.backgroundColor = editHeaderColor;
			tdElement.childNodes(1).style.backgroundColor = editHeaderColor;
		}

		if (IntFrom(oTmp.style.width) == 0)
			tdElement.style.border = "0";

		if(tdElement.childNodes(0).ControlType == "HDate")
		{
			SetHDateValueDateTime(oTmp.childNodes(0), (oTmp.DefaultValue != null)?oTmp.DefaultValue:"");
			FormatNewLineControl(oTmp);
		}
		if(tdElement.childNodes(0).ControlType == "HInput")
		{
			oTmp.value = (oTmp.DefaultValue != null)?ShortInitialFormat(oTmp, oTmp.DefaultValue):"";
			oTmp.InitialValue = (oTmp.DefaultValue != null)?oTmp.DefaultValue:"";
			FormatNewLineControl(oTmp);
		}
		if(tdElement.childNodes(0).ControlType == "HSelect")
		{
			var ctrlSelect = oTmp.parentElement.childNodes(1);
			if (oTmp.DefaultValue)
			{
				var nLen = ctrlSelect.options.length;
				for (var k=0; k<nLen; ++k)
					if ((k == 0 || ctrlSelect.options[k].value != "") && ctrlSelect.options[k].value == oTmp.DefaultValue)
					{
						SetHSelectValue(oTmp, ctrlSelect, k);
						break;
					}
			}
			else
			{
				oTmp.value = "";
				if (ctrlSelect.options.length > 0)
					ctrlSelect.options[0].selected = true;
			}
			FormatNewLineControl(oTmp);
			ShowHideHSelect(oTmp, ctrlSelect);
		}
		if(tdElement.childNodes(0).ControlType == "HSelection")
		{
			oTmp.InitialValue = oTmp.value = (oTmp.DefaultValue != null)?oTmp.DefaultValue:"";
			FormatNewLineControl(oTmp);
			if (oTmp.parentElement.childNodes(1).style.display == "")
			{
				oTmp.style.width = IntFrom(oTmp.style.width) + IntFrom(oTmp.parentElement.childNodes(1).style.width) + 1;
				oTmp.parentElement.childNodes(1).style.display = "none";
			}
		}
		if(tdElement.childNodes(0).ControlType == "HCheckBox")
		{
			oTmp.checked = (oTmp.DefaultValue == true);
			FormatNewLineControl(oTmp);
		}
	}

	var gChilds = oGrid.rows;
	var hChilds = gChilds(gChilds.length-2).cells(0).childNodes;
	hChilds(0).style.width = IntFrom(hChilds(0).style.width) - IntFrom(hChilds(1).style.width);
	hChilds(1).style.display = "";

	var nFirst = 0;
	var gridId = trHeader.childNodes(0).childNodes(0).GridId;
	var inpStatus = document.all(gridId + "ChangeFlag");
	var iPos = IntFrom(inpStatus.index);
	var gridValues = new Array();
	gridValues = arrMatrix[iPos];

	hChilds(0).value = IntFrom(gridValues.length) + 1;
	gChilds(gChilds.length-1).childNodes(0).style.backgroundColor = gChilds(gChilds.length-1).childNodes(0).childNodes(0).style.backgroundColor;
	gChilds(gChilds.length-1).cells(0).childNodes(0).value = "";

	var gridOuter = GetGridSpan(gChilds(0).cells(0).childNodes(0).GridId);
	if (gridOuter.MaxRowsCount != null && gChilds.length > IntFrom(gridOuter.MaxRowsCount))
			gChilds(gChilds.length-1).style.display = "none";

	ResizeGrid(oGrid);

	if (document.all(gridId + "InvokeOnAddNewLine") != null)
		window.execScript(document.all(gridId + "InvokeOnAddNewLine").value, "JavaScript");
}

function ResizeGrid(oGrid)
{
	var oParent = oGrid.parentElement;
	var styleGridParent = oParent.style;
	var nWidth = (oGrid.offsetWidth > nWidth) ? oGrid.offsetWidth : oParent.parentElement.childNodes(0).childNodes(0).offsetWidth;

	var nWidth = oParent.parentElement.childNodes(0).childNodes(0).offsetWidth;
	if (oGrid.offsetWidth > nWidth)
		nWidth = oGrid.offsetWidth;
	if (oParent.SpanWidth == "")
		oParent.SpanWidth = nWidth;
	if (oGrid.offsetHeight > IntFrom(oParent.SpanHeight))
	{
		if (oParent.ScrolledY == "")
		{
			styleGridParent.height = IntFrom(oParent.SpanHeight);
			styleGridParent.width = IntFrom(oParent.SpanWidth) + 17;
			oParent.ScrolledY = "auto";
		}
	}
	else
	{
		oParent.ScrolledY = "";
		styleGridParent.height = (oParent.ScrolledX == "")?oGrid.offsetHeight:oGrid.offsetHeight + 17;
	}

	if (IntFrom(oParent.SpanWidth) < oGrid.offsetWidth && oParent.ScrolledX == "")
	{
		styleGridParent.height = oParent.offsetHeight + 17;
		styleGridParent.overflowX = oParent.ScrolledX = "auto";
	}

	if (oParent.offsetHeight - 17 < IntFrom(oParent.SpanHeight) && oParent.ScrolledY == "")
		styleGridParent.width = IntFrom(oParent.SpanWidth);
}

function FocusNextHItem(trElement,cPos)
{
	var b = false;
	var i = cPos+1;
	while(!b && i<trElement.cells.length)
		b = FocusGenericHItem(trElement.childNodes(i++).childNodes(0));
	return b;
}

function FocusGenericHItem(objElem)
{
	var b = false;
	if (objElem.ControlReadOnly != true && objElem.ControlReadOnly != "true" && objElem.style.display == "")
	{
		var strCType = objElem.ControlType;
		if (strCType == "HInput" || strCType == "HSelect" || strCType == "HCheckBox" || strCType == "HSelection")
		{
			objElem.focus();
			b = true;
		}
		else
		if (strCType == "HDate")
		{
			objElem.childNodes(0).focus();
			b = true;
		}
	}
	return b;
}

function FocusNextLineFirstHItem(trElement, cPos, rPos)
{
	var b = false;
	if (trElement.parentElement.rows(rPos+1).style.display == "none")
		return null;

	var i = 1;
	
	while(!b && i<trElement.cells.length)
		b = FocusGenericHItem(trElement.parentElement.rows(rPos+1).childNodes(i++).childNodes(0));
	return b;

	if (!b && rPos == trElement.parentElement.rows.length - 1)
		b = null;

	return b;
}

function MoveUpHItem(oTmp)
{
	var trElement = (oTmp.ControlType == "HDate")?oTmp.parentElement.parentElement.parentElement:oTmp.parentElement.parentElement;
	var oTD = (oTmp.ControlType == "HDate")?oTmp.parentElement.parentElement:oTmp.parentElement;

	var b = false;
	while(!b && trElement.rowIndex > 0)
	{
		var upNextControlReadOnly = trElement.parentElement.rows(trElement.rowIndex-1).cells(oTD.cellIndex).childNodes(0).ControlReadOnly;
		if (upNextControlReadOnly != true && upNextControlReadOnly != "true")
			b = true;
		else
			trElement = trElement.parentElement.rows(trElement.rowIndex-1);
	}
	if (b)
		SetFocusHItem(trElement.parentElement.rows(trElement.rowIndex-1).cells(oTD.cellIndex));
}

function MoveDownHItem(oTmp)
{
	var trElement = (oTmp.ControlType == "HDate")?oTmp.parentElement.parentElement.parentElement:oTmp.parentElement.parentElement;
	var oTD = (oTmp.ControlType == "HDate")?oTmp.parentElement.parentElement:oTmp.parentElement;
	var rowColl = trElement.parentElement.rows;

	if ((rowColl(trElement.parentElement.rows.length-1).style.display == "" && trElement.rowIndex < rowColl.length - 1) ||
		(rowColl(trElement.parentElement.rows.length-1).style.display == "none" && trElement.rowIndex < rowColl.length - 2))
	{
		var b = false;
		while(!b && trElement.rowIndex < rowColl.length - 1)
		{
			var downNextControlReadOnly = rowColl(trElement.rowIndex+1).cells(oTD.cellIndex).childNodes(0).ControlReadOnly;
			if (downNextControlReadOnly == true || downNextControlReadOnly == "true")
				trElement = trElement.parentElement.rows(trElement.rowIndex+1);
			else
				b = true;
		}
		if (b)
			SetFocusHItem(trElement.parentElement.rows(trElement.rowIndex+1).cells(oTD.cellIndex));
	}
}

function ValidateLine(oTmp, tElement)
{
	var trElement = oTmp.parentElement.parentElement;
	var toElement = (tElement == null)?event.toElement:tElement;
	if (toElement != null)
	{
		if (toElement.parentElement != null && toElement.parentElement.ControlType == "HDate")
			toElement = toElement.parentElement;
		if (toElement.GridId == oTmp.GridId && toElement.parentElement.parentElement == trElement &&
			(toElement.parentElement.cellIndex == 0 && toElement == toElement.parentElement.childNodes(1)
			|| toElement.parentElement.cellIndex != 0 && toElement.ControlReadOnly != "true" && toElement.ControlReadOnly != true))
			return true;
	}
	for (var i=1; i<trElement.childNodes.length; ++i)
	{
		var tempoTmp = trElement.childNodes(i).childNodes(0);
		if (!ValidateControlInLine(tempoTmp, trElement))
			return false;
	}
	if (trElement.childNodes(0).childNodes(0).LineAdded == "false")
		AddLine(trElement);
	return true;
}

function ValidateNonHDateControl(oTmp, trElement)
{
	alert(oTmp.AlertRequiredString);
	window.event.returnValue = false;
	oTmp.focus();
	oTmp.fireEvent("onactivate");
	var spanObj = trElement.parentElement.parentElement.parentElement;
	if (oTmp.parentElement.offsetLeft + oTmp.offsetWidth > spanObj.offsetLeft + IntFrom(spanObj.style.width))
		spanObj.scrollLeft = IntFrom(spanObj.scrollLeft) + oTmp.offsetWidth;
}

function ValidateHDateControl(oTmp, trElement)
{
	alert(oTmp.AlertRequiredString);
	window.event.returnValue = false;
	if (oTmp.childNodes(0).style.display == "none")
	{
		oTmp.childNodes(0).style.display = "";
		oTmp.childNodes(1).style.display = "none";
	}
	oTmp.childNodes(0).focus();

	var spanObj = trElement.parentElement.parentElement.parentElement;
	if (oTmp.parentElement.offsetLeft + oTmp.offsetWidth > spanObj.offsetLeft + IntFrom(spanObj.style.width))
		spanObj.scrollLeft = IntFrom(spanObj.scrollLeft) + oTmp.offsetWidth;
}

function ValidateControlInLine(oTmp, trElement)
{
	if (oTmp.ControlType != "HDate")
	{
		if (oTmp.Required && oTmp.value == "" && oTmp.DefaultValue != "")
		{
			ValidateNonHDateControl(oTmp, trElement);
			return false;
		}
	}
	else
	{
		if (oTmp.Required && oTmp.childNodes(0).value == "--/--/----")
		{
			ValidateHDateControl(oTmp, trElement);
			return false;
		}
		//else if(typeof(OnBeforeDeactivateStartDate) != "undefined") OnBeforeDeactivateStartDate(g_currentDateCtl);
	}
	return true;
}

function SetFocusHItem(oTD)
{
	if(oTD.childNodes(0).ControlType == "HDate")
		oTD.childNodes(0).childNodes(0).focus();
	else
		oTD.childNodes(0).focus();
}

function UnformatHInput(strValue,strSufix,strPrefix,strComma,strDecimal, bFinal)
{
	if (strValue == "")
		return "";

	strValue = (strSufix != "" && strValue.indexOf(strSufix)>=0)?strValue.substr(0,strValue.length-strSufix.length):strValue;
	strValue = (strPrefix != "" && strValue.indexOf(strPrefix)>=0)?strValue.substr(strPrefix.length):strValue;
	if (strComma != "")
		strValue = strValue.replace(eval("/(\\" + strComma + ")/g"), "");
	if (strComma != "")
		strValue = strValue.replace(eval("/(\\" + strDecimal + ")/g"), ".");

	return strValue;
}

function FormatHInput(strValue,strSufix,strPrefix,strComma,strDecimal,strnDecimals,strNumericFieldType)
{
	if (strValue == "")
		return "";

	if (strNumericFieldType.toUpperCase() == "BYTE" || strNumericFieldType.toUpperCase() == "INT16" || strNumericFieldType.toUpperCase() == "INT32" || strNumericFieldType.toUpperCase() == "DECIMAL")
	{
		if(strDecimal != "")
			strValue = strValue.replace(eval("/(\\" + strDecimal + ")/g"), String.fromCharCode(2));
		strValue = strValue.replace(eval("/(" + String.fromCharCode(2) + ")/g"), ".");
		window.execScript("objForScript = FormatNumber("+strValue+", "+strnDecimals+", True, False, True)", "VBScript");

		var bPrim = true;
		for (var k=objForScript.length-1; k>=0; --k)
			if ((objForScript.charCodeAt(k) <= 47 || objForScript.charCodeAt(k) >= 58) && objForScript.charCodeAt(k)!= 45)
			{
				if (bPrim && strnDecimals != null && strnDecimals != "" && IntFrom(strnDecimals) > 0)
				{
					objForScript = objForScript.substring(0, k) + strDecimal + objForScript.substring(k+1);
					bPrim = false;
				}
				else
					objForScript = objForScript.substring(0, k) + strComma + objForScript.substring(k+1);
			}
		strValue = objForScript;
	}
	
	strValue = strPrefix + strValue + strSufix;
	return strValue;
}

function ValidateHInput(oTmp)
{
	if (IsCtrRO(oTmp))
		return;

	var strAlert = oTmp.AlertInvalidString;
	var bOK = true;
	var strValue = oTmp.value;

	if (oTmp.NumericFieldType.toUpperCase() == "DECIMAL" && strValue.substr(strValue.length-1) == oTmp.Decimal)
		oTmp.value = strValue += "0";

	if (strValue == "")
	{
		if (oTmp.Required != "true" && oTmp.Required != true && oTmp.DefaultValue != "" && oTmp.DefaultValue != null)
			oTmp.value = oTmp.DefaultValue;

		if (oTmp.InvokeOnChange != null && !DoCallExecScript(oTmp.InvokeOnChange,oTmp, true, true, true))
			return false;

		return (oTmp.Required && oTmp.DefaultValue != "" && event.toElement != oTmp.parentElement.parentElement.cells(0).childNodes(1))?DoCallExecScriptAndInvalidate(oTmp.AlertRequiredString, oTmp):true;
	}

	if (oTmp.NumericFieldType.toUpperCase() == "DECIMAL")
	{
		if(strValue == "-" || strValue.indexOf("-") != strValue.lastIndexOf("-")||
			strValue == oTmp.Decimal || strValue.indexOf(oTmp.Decimal) != strValue.lastIndexOf(oTmp.Decimal))
			bOK = false;

		strValue = strValue.replace(eval("/(\\" + oTmp.Decimal + ")/g"), "");
		for (var k=0; k<strValue.length; ++k)
			if ((strValue.charCodeAt(k) <= 47 || strValue.charCodeAt(k) >= 58) && strValue.charCodeAt(k)!= 45)
				bOK = false;
		
		if(strValue == "-" || strValue.indexOf("-") != strValue.lastIndexOf("-"))
			bOK = false;
	}
	if (!bOK)
		return DoCallExecScriptAndInvalidate(strAlert, oTmp);

	var strNumericType = oTmp.NumericFieldType.toUpperCase();

	if(strNumericType == "STRING" && oTmp.MaxLen != null && oTmp.value.length > IntFrom(oTmp.MaxLen) ||
	strNumericType == "BYTE" && (IntFrom(oTmp.value) < 0 || IntFrom(oTmp.value) > 255) ||
	strNumericType == "INT16" && (IntFrom(oTmp.value) < 0 || IntFrom(oTmp.value) > 255) ||
	strNumericType == "INT32" && (parseFloat(oTmp.value) < -2147483648 || parseFloat(oTmp.value) > 2147483467) ||
	strNumericType == "DECIMAL" && (parseFloat(oTmp.value) + Math.pow(10,28) + 1 < 0 || parseFloat(oTmp.value) - Math.pow(10,28) + 1 > 0) ||
	strNumericType == "DECIMAL" && ((oTmp.Positive != null && parseFloat(oTmp.value) < 0 && oTmp.Positive != "null") || (oTmp.StrictPositive != null && parseFloat(oTmp.value) <= 0) || (oTmp.Negative != null && parseFloat(oTmp.value) > 0) || (oTmp.StrictNegative != null && parseFloat(oTmp.value) >= 0)))
		bOK = false;

	if (!bOK)
		return DoCallExecScriptAndInvalidate(strAlert, oTmp);

	if (oTmp.InvokeOnChange != null && !DoCallExecScript(oTmp.InvokeOnChange, oTmp, true, true))
			return false;

	if (oTmp.GridControl == "true" && !VerifyIfUnicity(oTmp))
		return DoCallExecScriptAndInvalidate(document.all(oTmp.GridId + "AlertUnique").value, oTmp);

	return true;
}

function SaveGrid(gridId)
{
var arrMatrix = arrayGridSource;
var arrNewValues = arrayGridChanges;

	var inpStatus = document.all(gridId + "ChangeFlag");

	if (inpStatus.value != "changed")
		return;
	var iPos = IntFrom(inpStatus.index);
	var gridOuter = GetGridSpan(gridId);
	var oGrid = gridOuter.childNodes(1).childNodes(0);
	var gridValues = new Array();
	gridValues = arrMatrix[iPos];
	var arrNewLine = new Array();
	arrNewLine = arrNewValues[iPos];

	var nFirst = 0;
	var nLast = gridValues.length;

	if (document.all(gridId +"Paging") != null)
	{
		var nPageSize = IntFrom(document.all(gridId +"Paging").PageSize);
		var nPageIndex = IntFrom(document.all(gridId +"Paging").PageIndex);

		nFirst = nPageSize * nPageIndex;
		var nCurrentPageSize = oGrid.rows.length;
		var bFooterVisible = (oGrid.rows(oGrid.rows.length - 1).style.display == "");
		nLast = nFirst + nCurrentPageSize - (!bFooterVisible ? 1 : 0);
		if (nLast > gridValues.length)
			nLast = gridValues.length;
	}

	var matrix = new Array();

	for (var k=0; k<nLast-nFirst; ++k)
	{
		var arr = new Array();
		var bDirty = false;
		var i = k + nFirst;
		var j = 0;
		var strVal = "";
		while (j<oGrid.rows[0].cells.length-1)
		{
			var oTmp = oGrid.rows[k].cells[j+1].childNodes(0);
			if(oTmp.ControlType == "HInput")
			{
				strVal = UnformatHInput(oTmp.value,oTmp.Sufix,oTmp.Prefix,oTmp.Comma,oTmp.Decimal,true);
				if (IsSameValue(gridValues, strVal, i, j, 0))
					bDirty = true;
			}
			if(oTmp.ControlType == "HCheckBox")
			{
				strVal = (oTmp.checked)?"TRUE":"FALSE";
				if (IsSameValue(gridValues, strVal, i, j, 2))
					bDirty = true;
			}
			if(oTmp.ControlType == "HSelect")
			{
				strVal = oTmp.CodeValue;
				if (IsSameValue(gridValues, strVal, i, j, 0))
					bDirty = true;
			}
			if(oTmp.ControlType == "HSelection")
			{
				strVal = oTmp.value;
				if (IsSameValue(gridValues, strVal, i, j, 0))
					bDirty = true;
			}
			if(oTmp.ControlType == "HDate")
			{
				strVal = GetHDateValueDateTime(oTmp.childNodes(0));
				if (IsSameValue(gridValues, strVal, i, j, 1))
					bDirty = true;
			}
			j++;
		}
		
		if (bDirty)
		{
			for (var s=0; s<gridValues[i].length; ++s)
				arr[s] = gridValues[i][s];
			arr[arr.length] = i;
			matrix[matrix.length] = arr;
		}
	}

	for (var i=0; i<matrix.length; ++i)
	{
		var bIndexFound = false;
		var j = 0;
		while (j<arrNewLine.length && !bIndexFound)
		{
			if (matrix[i][matrix[i].length-1] == arrNewLine[j][arrNewLine[j].length-1])
			{
				arrNewLine[j] = matrix[i];
				j = arrNewLine.length;
				bIndexFound = true;
			}
			j++;
		}
		if (!bIndexFound)
			arrNewLine[arrNewLine.length] = matrix[i];
	}

	arrNewValues[iPos] = arrNewLine;

	inpStatus.value = "unsaved";
}

function SendChanges(gridId)
{
var arrNewValues = arrayGridChanges;

	var gridOuter = GetGridSpan(gridId);
	if (gridOuter == null)
		return;

	var inpStatus = document.all(gridId + "ChangeFlag");
	if (inpStatus.value == "changed")
		SaveGrid(gridId);
	if (inpStatus.value != "unsaved")
		return;

	var iPos = IntFrom(inpStatus.index);
	var gridValues = arrNewValues[iPos];
	for (var i=0; i<gridValues.length; ++i)
		for (var j=0; j<gridValues[0].length; ++j)
			if (gridValues[i][j] != "" && gridValues[i][j] != null)
				gridValues[i][j] = gridValues[i][j].toString().replace(/(&)/g, "&amp;").replace(/(")/g, "&quot;").replace(/(')/g, "&apos;").replace(/(<)/g, "&lt;").replace(/(>)/g, "&gt;");

	var strgridValues = DoPack(gridValues);
	strgridValues = ReplaceRowCellSeparators(strgridValues);

	var xParams = new Array(gridId, strgridValues);
	var xResult = DoCallWebService_Grid("SaveChanges_NEW", xParams);
	if (xResult != null)
	{
		document.all(gridId + "ChangeFlag").value = "unchanged";
		arrNewValues[iPos] = new Array();
	}
}

function GridBlur(gridId)
{
	if (event.toElement != null && document.all("SpanContentGrid" + gridId).contains(event.toElement))
		return;

	SelectControlHeaders();
	SaveGrid(gridId);
}

function SaveSendGrid(gridId)
{
	var strValue = document.all(gridId + "ChangeFlag").value;
	
	if (strValue == "changed")
		SaveGrid(gridId);
	//strValue = document.all(gridId + "ChangeFlag").value;
	//if (strValue == "unsaved")
		//SendChanges(gridId);
	return document.all(gridId + "ChangeFlag");
}

function GetGridObject(gridId)
{
	return document.all(gridId + "BigSpan").childNodes(1).childNodes(0);
}

function GetGridHeaderObject(gridId)
{
	return document.all(gridId + "BigSpan").childNodes(1).childNodes(0).parentElement.parentElement.childNodes(0).childNodes(0);
}

function GetGridSpan(gridId)
{
	return document.all(gridId + "BigSpan");
}

function DeleteLine(tempObj)
{
	var arrMatrix = arrayGridSource;
	SelectControlHeaders(null);
	var trElement = tempObj.parentElement.parentElement;
	if (trElement.rowIndex == trElement.parentElement.childNodes.length-1)
		return;
	var gridId = tempObj.GridId;

	if (trElement.rowIndex == trElement.parentElement.childNodes.length - 2 && trElement.childNodes(0).childNodes(0).LineAdded == "false")
	{
		if (document.all(gridId + "InvokeOnDeleteNewLine") != null)
			window.execScript(document.all(gridId + "InvokeOnDeleteNewLine").value, "JavaScript");

		trElement.removeNode(true);
		var oGrid = GetGridObject(gridId);
		var gridOuter = GetGridSpan(gridId);
		if (gridOuter.MaxRowsCount != null && oGrid.rows.length <= IntFrom(gridOuter.MaxRowsCount))
			oGrid.rows(oGrid.rows.length - 1).style.display = "";
		ResizeGrid(oGrid);
		return;
	}
	var i = 1;
	while  (i<trElement.cells.length-1)
	{
		var cellChilds = trElement.cells(i++).childNodes;
		if (cellChilds(0).InitialValue != null)
			cellChilds(0).value = cellChilds(0).InitialValue;
		if (cellChilds(0).InitialSelectedIndex != null)
			SetHSelectValue(cellChilds(0), cellChilds(1), IntFrom(cellChilds(0).InitialSelectedIndex));
	}

	//var inpStatus = SaveSendGrid(gridId);

	if (document.all(gridId + "InvokeOnDeleteLine") != null && !DoCallExecScript(document.all(gridId + "InvokeOnDeleteLine").value))
		return false;

	var nFirst = 0;
	var nLast = 0;

	if (document.all(gridId + "Paging") != null)
	{
		var nPageSize = IntFrom(document.all(gridId + "Paging").PageSize);
		var nPageIndex = IntFrom(document.all(gridId + "Paging").PageIndex);
		var inpStatus = document.all(gridId + "ChangeFlag");
		var iPos = IntFrom(inpStatus.index);
		var gridValues = new Array();
		gridValues = arrMatrix[iPos];
		if (nPageIndex == 0)
		{
			nFirst = 0;
			nLast = (nPageSize <= gridValues.length) ? nPageSize: gridValues.length;
		}
		else
		{
			nFirst = nPageSize * nPageIndex;
			nLast = (nPageSize * (nPageIndex + 1) < gridValues.length) ? nPageSize * (nPageIndex + 1) : gridValues.length;
		}
	}

	var gridOuter = GetGridSpan(gridId);
	var oGrid = GetGridObject(gridId);
	var iIndex = tempObj.parentElement.parentElement.rowIndex;

	var xParams = new Array(gridId, iIndex + nFirst);
	var xResult = DoCallWebService_Grid("DeleteLine_NEW", xParams);
	if (xResult != null)
	{
		var inpStatus = document.all(gridId + "ChangeFlag");
		inpStatus.value = "unchanged";

		var iPos = IntFrom(inpStatus.index);
		var gridValues = new Array();
		gridValues = arrMatrix[iPos];
		gridValues.splice(iIndex + nFirst, 1);

		var inpReadOnly = document.all(gridId + "ReadOnlyLines");

		if (inpReadOnly.value != "")
			inpReadOnly.value = xResult[0];

		var nFirst = 0;

		if (document.all(gridId + "Paging") != null)
		{
			var nPageSize = IntFrom(document.all(gridId + "Paging").PageSize);
			var nPageIndex = IntFrom(document.all(gridId + "Paging").PageIndex);
			var inpStatus = document.all(gridId + "ChangeFlag");
			var iPos = IntFrom(inpStatus.index);
			var gridValues = new Array();
			gridValues = arrMatrix[iPos];
			if (nPageIndex == 0)
			{
				nFirst = 0;
				nLast = (nPageSize <= gridValues.length) ? nPageSize : gridValues.length;
			}
			else
			{
				nFirst = nPageSize * nPageIndex;
				nLast = (nPageSize * (nPageIndex + 1) < gridValues.length) ? nPageSize * (nPageIndex + 1) : gridValues.length;
			}
		}

		var oGrid = GetGridObject(gridId);
		oGrid.rows(iIndex).removeNode(true);
		for (var k=iIndex; k<oGrid.rows.length-1; ++k)
			oGrid.rows(k).childNodes(0).childNodes(0).value = nFirst + k + 1;

		if (GetGridSpan(gridId).MaxRowsCount != null)
			if (oGrid.rows.length <= IntFrom(GetGridSpan(gridId).MaxRowsCount))
				oGrid.rows(oGrid.rows.length-1).style.display = "";

		ResizeGrid(oGrid);
	}
}

function GetHControlValue(oTmp)
{
	var strVal = "";
	switch (oTmp.ControlType)
	{
		case "HDate":
			strVal = GetHDateValueDateTime(oTmp.childNodes(0));
			break;
		case "HInput":
			strVal = UnformatHInput(oTmp.value, oTmp.Sufix, oTmp.Prefix, oTmp.Comma, oTmp.Decimal, true);
			break;
		case "HSelect":
			ctrlSelect = oTmp.parentElement.childNodes(1);
			if (ctrlSelect.selectedIndex >= 0)
				strVal = ctrlSelect.options[ctrlSelect.selectedIndex].value;
			else
				strVal = "";
			break;
		case "HSelection":
			strVal = oTmp.value;
			break;
		case "HCheckBox":
			strVal = (oTmp.checked ? "true" : "false");
			break;
	}
	return strVal;
}

function AddLine(trElement)
{
var arrMatrix = arrayGridSource;

	var gridId = trElement.childNodes(0).childNodes(0).GridId;
	var inpStatus = SaveSendGrid(gridId);
	var gridOuter = GetGridSpan(gridId);
	var oGrid = gridOuter.childNodes(1).childNodes(0);
	var gridValues = arrMatrix[IntFrom(inpStatus.index)];
	var arr = new Array();
	var j = 0;
	
	while (j<oGrid.rows[0].cells.length-1)
	{
		arr[j] = GetHControlValue(oGrid.rows[oGrid.rows.length - 2].cells[j + 1].childNodes(0));
		j++;
	}

	newLineArray = arr;

	for (var i=0; i<arr.length; ++i)
		arr[i] = arr[i].toString().replace(/(&)/g, "&amp;").replace(/(")/g, "&quot;").replace(/(')/g, "&apos;").replace(/(<)/g, "&lt;").replace(/(>)/g, "&gt;");

	var xTempArray = new Array();
	xTempArray[0] = arr;
	var strTempArray = DoPack(xTempArray);
	strTempArray = ReplaceRowCellSeparators(strTempArray);

	var xParams = new Array(gridId, strTempArray);
	var xResult = DoCallWebService_Grid("AddNewLine_NEW", xParams);
	if (xResult != null)
	{
		var inpStatus = document.all(gridId + "ChangeFlag")
		inpStatus.value = "unchanged";
		var iPos = IntFrom(inpStatus.index);
		var gridValues = new Array();
		gridValues = arrMatrix[iPos];
		gridValues[gridValues.length] = newLineArray;

		var inpReadOnly = document.all(gridId + "ReadOnlyLines");

		if (inpReadOnly.value != "")
			inpReadOnly.value = xResult[0];

		var trElement = oGrid.rows(oGrid.rows.length - 2);
		trElement.childNodes(0).childNodes(0).LineAdded = "true";
		if (document.all(gridId + "InvokeOnAddLine") != null)
			window.execScript(document.all(gridId + "InvokeOnAddLine").value, "JavaScript");
	}
	else
	{
		currentHGridSel = null;
		oGrid.rows(oGrid.rows.length - 2).removeNode(true);
		ResizeGrid(oGrid);
	}
}

function ClientSortGrid(hdObj)
{
	var oGrid = GetGridObject(hdObj.GridId);
	var inpStatus = SaveSendGrid(hdObj.GridId);
	var strSort = "";

	document.all(hdObj.GridId + "SortString").value = hdObj.childNodes(1).SortString;

	if (hdObj.childNodes(1).style.display == "none")
		strSort = hdObj.childNodes(1).SortString;
	else
		strSort = hdObj.childNodes(2).SortString;

	SortGrid(strSort, hdObj.GridId);
}

function OnHControlKeyDown(oTmp)
{
	var keyCode = event.keyCode;
	
	if (IsCtrRO(oTmp))
	{
		event.returnValue = false;
		return;
	}
	if (oTmp.ControlType == "HCheckBox" && keyCode == 32)
	{
		event.returnValue = false;
		oTmp.click();
	}
	if (keyCode == 27)
	{
		oTmp.value = oTmp.InitialValue;
		event.returnValue = false;
	}

	if (oTmp.GridControl)
	{
		switch (keyCode)
		{
			case 38:
				if(oTmp.ControlType != "HSelect")
				{
					event.returnValue = false;
					MoveUpHItem(oTmp);
				}
				break;				
			case 40:
				if(oTmp.ControlType != "HSelect")
				{
					event.returnValue = false;
					MoveDownHItem(oTmp);
				}
				break;				
			case 13:
				event.returnValue = false;
				var trElement = oTmp.parentElement.parentElement;

				if (trElement.rowIndex < trElement.parentElement.rows.length)
				{
					var cPos = oTmp.parentElement.cellIndex;
					var b = false;

					if (cPos < trElement.cells.length - 1)
						b = FocusNextHItem(trElement,cPos);

					if ((cPos >= trElement.cells.length - 1) || !b)
					{
						if (trElement.rowIndex < trElement.parentElement.rows.length - 1)
							b = FocusNextLineFirstHItem(trElement, cPos, trElement.rowIndex);
						while(b != true && trElement.rowIndex < trElement.parentElement.rows.length - 1 && trElement.parentElement.rows(trElement.rowIndex+1).style.display == "")
						{
							trElement = trElement.parentElement.rows(trElement.rowIndex+1);
							b = FocusNextLineFirstHItem(trElement, cPos, trElement.rowIndex);
						}
					}
				}
				break;
			}				
		}
		else
			if (keyCode == 13)
				event.returnValue = false;
}

function OnHCheckBoxBlur(oTmp)
{
	if (FocusIsInGrid(oTmp))
		return;

	if (oTmp.checked && !VerifyIfUnicity(oTmp))
		return DoCallExecScriptAndInvalidate(document.all(oTmp.GridId + "AlertUnique").value, oTmp);

	if (oTmp.InvokeOnBeforeDeactivate != null && !DoCallExecScript(oTmp.InvokeOnBeforeDeactivate, oTmp, true, true))
		return false;

	if (oTmp.GridControl)
	{
		var trElement = oTmp.parentElement.parentElement;
		if (trElement.rowIndex == trElement.parentElement.childNodes.length - 2 && !ValidateLine(oTmp))
				return;
		SetBlurOnElem(oTmp);
	}

	var rowHeader = document.all(oTmp.GridId + "HeaderSpan").childNodes(0).rows(0);
	var nIndex = oTmp.parentElement.cellIndex - 1;

	if (rowHeader.cells(nIndex+1).childNodes(0).UniqueValue == "true")
		SaveGrid(oTmp.GridId);
}

function OnHCheckBoxFocus(oTmp)
{
	if (oTmp.GridControl && (document.all(oTmp.GridId + "InvokeOnChangeLine") != null))
		window.execScript(document.all(oTmp.GridId + "InvokeOnChangeLine").value, "JavaScript");

	if (oTmp.InvokeOnActivate != null && !DoCallExecScript(oTmp.InvokeOnActivate, oTmp, true, false, false))
		return false;

	SelectControlHeaders(oTmp);

	if (IsCtrRO(oTmp))
		return;

	if (oTmp.GridControl)
		SetFocusOnElem(oTmp);
}

function OnHCheckBoxClick(oTmp)
{
	var trElement = oTmp.parentElement.parentElement;

	if (trElement.rowIndex != trElement.parentElement.rows.length - 2 || (trElement.rowIndex == trElement.parentElement.rows.length - 2 && trElement.childNodes(0).childNodes(0).LineAdded == "true"))
		document.all(oTmp.GridId + "ChangeFlag").value = "changed";

	if (oTmp.InvokeOnChange != null)
		window.execScript(oTmp.InvokeOnChange, "JavaScript");

	if (trElement.rowIndex != trElement.parentElement.rows.length - 1)
		return;
	InsertBlankRow(GetGridObject(oTmp.GridId));
}

function HGridScroll(oTmp)
{
	oTmp.parentElement.childNodes(0).scrollLeft = oTmp.scrollLeft;

	if (currentHGridElem != null)
		HGridScrollActivate();
}

function HGridActivate()
{
	if (FocusIsInGrid2())
		HGridScrollActivate();
}

function HGridScrollActivate()
{
	window.event.returnValue = false;
	if (currentHGridElem.ControlType == "HDate")
	{
		if (g_currentDateCtl != null)
			SelectLastHDateControl(1);
	}
	else
		currentHGridElem.setActive();
}

function HGridCM()
{
	if (FocusIsInGrid2())
	{
		window.event.returnValue = false;
		if (currentHGridElem.ControlType != "HDate")
			currentHGridElem.setActive();
		else
			SelectLastHDateControl(g_SelectedInput);
	}
}

function OnHSelectFocus(oTmp)
{
	if (document.all(oTmp.GridId + "InvokeOnChangeLine") != null)
		window.execScript(document.all(oTmp.GridId + "InvokeOnChangeLine").value, "JavaScript");

	if (oTmp.InvokeOnActivate != null && !DoCallExecScript(oTmp.InvokeOnActivate, oTmp, true, false, false))
			return false;

	SelectControlHeaders(oTmp);

	if (IsCtrRO(oTmp))
		return;

	var ctrlSelect = oTmp.parentElement.childNodes(1);
	SetFocusOnElem(oTmp);
}

function OnHSelectChange(oTmp)
{
	if (oTmp.parentElement.childNodes(0).InvokeOnChange != null && !DoCallExecScript(oTmp.parentElement.childNodes(0).InvokeOnChange, oTmp, true, true, false))
		return false;

	if (oTmp.parentElement.parentElement.rowIndex == oTmp.parentElement.parentElement.parentElement.rows.length - 1)
		InsertBlankRow(GetGridObject(oTmp.parentElement.childNodes(0).GridId));
	else
		return;
}

function OnHSelectBlur(oTmp)
{
	if (FocusIsInGrid(oTmp))
		return;
	try
	{
	if (oTmp.options.length > 0 && oTmp.parentElement.childNodes(0).GridControl == "true" && oTmp.options[oTmp.selectedIndex].value != oTmp.parentElement.childNodes(0).CodeValue &&
		event != null && event.toElement != null && event.toElement != oTmp.parentElement.parentElement.cells(0).childNodes(1) &&
		!VerifyIfUnicity(oTmp.parentElement.childNodes(0)))
		return DoCallExecScriptAndInvalidate(document.all(oTmp.GridId + "AlertUnique").value, oTmp);

	if (oTmp.parentElement.childNodes(0).InvokeOnBeforeDeactivate != null && oTmp.parentElement.childNodes(0).InvokeOnBeforeDeactivate != "" && 
	!DoCallExecScript(oTmp.parentElement.childNodes(0).InvokeOnBeforeDeactivate, oTmp, true, true, false))
		return false;
	}catch(exception){}
	oTmp.style.display = "none";
	if (oTmp.selectedIndex >= 0)
	{
		var sIndex = oTmp.selectedIndex;
		var trElement = oTmp.parentElement.parentElement;
		if (oTmp.parentElement.childNodes(0).CodeValue != oTmp.options(sIndex).value)
		{
			if (trElement.rowIndex != trElement.parentElement.rows.length - 2 || (trElement.rowIndex == trElement.parentElement.rows.length - 2 && trElement.childNodes(0).childNodes(0).LineAdded == "true"))
				document.all(oTmp.GridId + "ChangeFlag").value = "changed";
			oTmp.parentElement.childNodes(0).value = oTmp.options(sIndex).text;
			oTmp.parentElement.childNodes(0).CodeValue = oTmp.options(sIndex).value;
			oTmp.parentElement.childNodes(0).title = (oTmp.parentElement.childNodes(0).ToolTip)?oTmp.options(sIndex).text:null;
			if (event.toElement != oTmp.parentElement.parentElement.childNodes(0).childNodes(1))
				oTmp.parentElement.childNodes(0).InitialSelectedIndex = sIndex;
		}
	}

	SetBlurOnElem(oTmp);

	if (oTmp.parentElement.parentElement.rowIndex == oTmp.parentElement.parentElement.parentElement.childNodes.length - 2 && !ValidateLine(oTmp))
		return;
}

function OnHSelectKeyDown(ctrlSelect, keyCode)
{
	oTmp = ctrlSelect.parentElement.childNodes(0);
	if (IsCtrRO(oTmp))
	{
		event.returnValue = false;
		return;
	}
	if ((keyCode == 9 && !event.shiftKey) || keyCode == 13)
	{
		event.returnValue = false;
		var trElement = oTmp.parentElement.parentElement;
		var rowColl = trElement.parentElement.rows;
		if (trElement.rowIndex < rowColl.length)
		{
			var cPos = oTmp.parentElement.cellIndex;
			var b = false;

			if (cPos < trElement.cells.length - 1)
				b = FocusNextHItem(trElement,cPos);

			if ((cPos >= trElement.cells.length - 1) || !b)
			{
				if (trElement.rowIndex < rowColl.length - 1)
					b = FocusNextLineFirstHItem(trElement, cPos, rPos);
				while(b != true && trElement.rowIndex < rowColl.length - 1 && rowColl(trElement.rowIndex+1).style.display == "")
				{
					trElement = rowColl(trElement.rowIndex+1);
					b = FocusNextLineFirstHItem(trElement, cPos, rPos);
				}
			}
			return;
		}
	}
}

function OnHSelectionFocus(oTmp)
{
	if (document.all(oTmp.GridId + "InvokeOnChangeLine") != null && !DoCallExecScript(document.all(oTmp.GridId + "InvokeOnChangeLine").value, oTmp, true, true, false))
		return false;

	if (oTmp.InvokeOnActivate != null && !DoCallExecScript(oTmp.InvokeOnActivate, oTmp, true, true, false))
			return false;

	SelectControlHeaders(oTmp);

	if (IsCtrRO(oTmp))
		return;
	if (oTmp.parentElement.childNodes(1).style.display == "")
	{
		SetFocusOnElem(oTmp);
		return;
	}
	SetFocusOnElem(oTmp, true);
	//if(typeof(OnSelect_BudgetDefinition) != 'undefined')
		//OnSelect_BudgetDefinition();
		//window.execScript(oTmp.InvokeOnChangeLine, "JavaScript");
}

function OnHSelectionBlur(oTmp)
{
	if (FocusIsInGrid(oTmp))
		return;

	var trElement = oTmp.parentElement.parentElement;

	if (oTmp.GridControl == "true")
	{
		if (event.toElement != trElement.childNodes(0).childNodes(1) && event.toElement!=oTmp.parentElement.childNodes(1) && 
		!VerifyIfUnicity(oTmp))
			return DoCallExecScriptAndInvalidate(document.all(oTmp.GridId + "AlertUnique").value, oTmp);
	}

	if (oTmp.InitialValue != oTmp.value)
	{
		if (oTmp.InvokeOnChange != null && !DoCallExecScript(oTmp.InvokeOnChange, oTmp, true, true, true))
			return false;
		oTmp.InitialValue = oTmp.value;
		if (trElement.rowIndex != trElement.parentElement.rows.length - 2 || (trElement.rowIndex == trElement.parentElement.rows.length - 2 && trElement.childNodes(0).childNodes(0).LineAdded == "true"))
			document.all(oTmp.GridId + "ChangeFlag").value = "changed";
	}

	if (oTmp.parentElement.contains(event.toElement))
	{
		SetBlurOnElem(oTmp);
		return;
	}

	if (oTmp.value == "" && oTmp.Required && oTmp.parentElement.parentElement.rowIndex != oTmp.parentElement.parentElement.parentElement.childNodes.length - 1
		&& event.toElement != oTmp.parentElement.parentElement.cells(0).childNodes(1))
			return DoCallExecScriptAndInvalidate(oTmp.AlertRequiredString, oTmp);

	if (trElement.rowIndex == trElement.parentElement.childNodes.length - 2 && event.toElement!=oTmp.parentElement.childNodes(1) && !ValidateLine(oTmp))
		return;

	
	SetBlurOnElem(oTmp, true);
}

function OnHSelectionKeyUp(oTmp)
{
	if (oTmp.ControlReadOnly != true && oTmp.ControlReadOnly != "true")
		if (event.keyCode == 8 || event.keyCode == 46)
		{
			if (oTmp.DependentColumns != null)
			{
				if (oTmp.ControlPartialReadOnly == true || oTmp.ControlPartialReadOnly == "true" || oTmp.value == "")
				{
					var arDependentColumns = oTmp.DependentColumns.toString().split(',');
					for (var i=0; i<arDependentColumns.length; ++i)
						SetGridControlValue(oTmp.GridId, oTmp.parentElement.parentElement.rowIndex, IntFrom(arDependentColumns[i]), "");
				}
				if (oTmp.ControlPartialReadOnly == true || oTmp.ControlPartialReadOnly == "true")
				{
					var stroTmpInitialValue = oTmp.InitialValue;
					SetHControlValue(oTmp, "");
					event.returnValue = false;

					if (stroTmpInitialValue != oTmp.value && oTmp.InvokeOnDeleteValue != null &&
					!DoCallExecScript(oTmp.InvokeOnDeleteValue, oTmp, true, true, false))
						return false;
				}
			}
		}

	//TODO Check this
	if (oTmp.value == "" || (oTmp.DefaultValue != null && oTmp.value == oTmp.DefaultValue) || 
			(oTmp.parentElement.parentElement.parentElement != null 
				&& oTmp.parentElement.parentElement.rowIndex != oTmp.parentElement.parentElement.parentElement.rows.length - 1))
		return;

	InsertBlankRow(GetGridObject(oTmp.GridId));
}

function OnHSelectionClick(oTmp)
{
	SetFocusOnElem(oTmp);

	var trElement = oTmp.parentElement.parentElement;
	if (trElement.rowIndex == trElement.parentElement.rows.length - 1)
		InsertBlankRow(GetGridObject(oTmp.GridId));

	window.execScript(oTmp.OnOpenSelection, "JavaScript");
}

function RefreshGrid(strFilter, arrColumns, childGridId)
{
	var arrIds = arrayGridId;
	var arrMatrix = arrayGridSource;
	var arrNewValues = arrayGridChanges;

	var inpStatus = SaveSendGrid(childGridId);

	var xColumns = new Array();
	xColumns[0] = arrColumns;
	var strColumns = DoPack(xColumns);
	strColumns = ReplaceRowCellSeparators(strColumns);

	var xParams = new Array(childGridId, strFilter, strColumns);
	var xResult = DoCallWebService_Grid("RequestSourceArray_NEW", xParams);
	if (xResult != null)
	{
		var inpStatus = document.all(childGridId + "ChangeFlag");
		if (inpStatus.index.toString() == "")
		{
			var gridValues = new Array();
			gridValues = SwitchStringToArray(document.all(childGridId).value);
			arrIds[arrIds.length] = childGridId;
			arrMatrix[arrMatrix.length] = gridValues;
			arrNewValues[arrNewValues.length] = new Array();
			inpStatus.index = arrIds.length - 1;
		}

		var iPos = IntFrom(inpStatus.index);
		arrMatrix[iPos] = SwitchStringToArray(xResult[0].replace(/(&quot;)/g, '"').replace(/(&apos;)/g, "'").replace(/(&lt;)/g, "<").replace(/(&gt;)/g, ">").replace(/(&amp;)/g, "&"));
		if (xResult[1] != "")
			arrayReadOnlySource = GetROValues(xResult[1]);
		var inpReadOnly = document.all(childGridId + "ReadOnlyLines");
		inpReadOnly.value = xResult[1];
		GenerateGrid(childGridId, true);
		arrayReadOnlySource = null;
	}

	if (document.all(childGridId + "InvokeOnRefreshGrid") != null)
		window.execScript(document.all(childGridId + "InvokeOnRefreshGrid").value, "JavaScript");
}

function SortGrid(strSort, childGridId)
{
var arrMatrix = arrayGridSource;

	var inpStatus = document.all(childGridId + "ChangeFlag");
	var tempElem = event.srcElement;
	if (tempElem.tagName == "INPUT")
		tempElem = tempElem.parentElement;
	if (inpStatus.value == "changed")
		SaveGrid(childGridId);
	if (inpStatus.value == "unsaved")
		SendChanges(childGridId);

	var xParams = new Array(childGridId, strSort);
	var xResult = DoCallWebService_Grid("RequestSortedSourceArray_NEW", xParams);
	if (xResult != null)
	{
		var inpStatus = document.all(childGridId + "ChangeFlag");
		var iPos = IntFrom(inpStatus.index);
		arrMatrix[iPos] = SwitchStringToArray(xResult[0].replace(/(&quot;)/g, '"').replace(/(&apos;)/g, "'").replace(/(&lt;)/g, "<").replace(/(&gt;)/g, ">").replace(/(&amp;)/g, "&"));
		if (xResult[1] != "")
			arrayReadOnlySource = GetROValues(xResult[1]);
		var inpReadOnly = document.all(childGridId + "ReadOnlyLines");
		inpReadOnly.value = xResult[1];

		GenerateGrid(childGridId, true);
		arrayReadOnlySource = null;

		var objRow = tempElem.parentElement.parentElement;
		var nIdx = tempElem.parentElement.cellIndex;

		for (var i=1; i<objRow.cells.length; ++i)
		{
			if (i != nIdx && objRow.cells(i).childNodes(0).childNodes.length == 3)
			{
				objRow.cells(i).childNodes(0).childNodes(1).style.display = "none";
				objRow.cells(i).childNodes(0).childNodes(2).style.display = "none";
			}
		}

		if (tempElem.childNodes(1).style.display == "none")
		{
			tempElem.childNodes(1).style.display = "";
			tempElem.childNodes(2).style.display = "none";
		}
		else
		{
			tempElem.childNodes(1).style.display = "none";
			tempElem.childNodes(2).style.display = "";
		}

		if (document.all(childGridId + "InvokeOnRefreshGrid") != null)
			window.execScript(document.all(childGridId + "InvokeOnRefreshGrid").value, "JavaScript");
	}
}

function DeleteFromSource(strFilter, childGridId)
{
var arrMatrix = arrayGridSource;

	var inpStatus = SaveSendGrid(childGridId);

	var xParams = new Array(childGridId, strFilter);
	var xResult = DoCallWebService_Grid("DeleteFromSource_NEW", xParams);
	if (xResult != null)
	{
		var inpStatus = document.all(childGridId + "ChangeFlag");
		inpStatus.value = "unchanged";
		var iPos = IntFrom(inpStatus.index);
		arrMatrix[iPos] = SwitchStringToArray("");
		GenerateGrid(childGridId, true);
	}
}

function InitialFormat(strValue,strSufix,strPrefix,strComma,strDecimal,strnDecimals,strNumericFieldType)
{
	if(strnDecimals == "-1")
		strnDecimals = "0";
		
	if (strNumericFieldType.toUpperCase() == "BYTE" || strNumericFieldType.toUpperCase() == "INT16" || strNumericFieldType.toUpperCase() == "INT32" || strNumericFieldType.toUpperCase() == "DECIMAL")
	{
		if(strNumericFieldType.toUpperCase() == "DECIMAL" && strValue == "")
			strValue = "0";
		window.execScript("objForScript = FormatNumber("+strValue+", "+strnDecimals+", True, False, True)", "VBScript");

		var bPrim = true;
		for (var k=objForScript.length-1; k>=0; --k)
			if ((objForScript.charCodeAt(k) <= 47 || objForScript.charCodeAt(k) >= 58) && objForScript.charCodeAt(k)!= 45)
			{
				if (bPrim && strnDecimals != null && strnDecimals != "" && IntFrom(strnDecimals) > 0)
				{
					objForScript = objForScript.substring(0, k) + strDecimal + objForScript.substring(k+1);
					bPrim = false;
				}
				else
					objForScript = objForScript.substring(0, k) + strComma + objForScript.substring(k+1);
			}
		strValue = objForScript;
	}
	
	strValue = strPrefix + strValue + strSufix;
	return strValue;
}

function SetHInputFormattedValue(objInput, strValue)
{
	objInput.value = (document.activeElement != objInput || objInput.readOnly)?GetFormattedValue(strValue, objInput.Sufix, objInput.Prefix, objInput.Comma, objInput.Decimal, objInput.nDecimals, objInput.NumericFieldType):strValue;
	objInput.title = (objInput.ToolTip != null)?objInput.value:null;
}

function GetFormattedValue(strValue,strSufix,strPrefix,strComma,strDecimal,strnDecimals,strNumericFieldType)
{
	if (strValue == "" || strValue == null)
		return "";

	if(strnDecimals == "-1")
		strnDecimals = "0";

	if (strNumericFieldType.toUpperCase() == "BYTE" || strNumericFieldType.toUpperCase() == "INT16" || strNumericFieldType.toUpperCase() == "INT32" || strNumericFieldType.toUpperCase() == "DECIMAL")
	{
		window.execScript("objForScript = FormatNumber(\""+strValue+"\", "+strnDecimals+", True, False, True)", "VBScript");

		var bPrim = true;
		for (var k=objForScript.length-1; k>=0; --k)
			if ((objForScript.charCodeAt(k) <= 47 || objForScript.charCodeAt(k) >= 58) && objForScript.charCodeAt(k)!= 45)
			{
				if (bPrim && strnDecimals != null && strnDecimals != "" && IntFrom(strnDecimals) > 0)
				{
					objForScript = objForScript.substring(0, k) + strDecimal + objForScript.substring(k+1);
					bPrim = false;
				}
				else
					objForScript = objForScript.substring(0, k) + strComma + objForScript.substring(k+1);
			}
		strValue = objForScript;
	}
	
	strValue = strPrefix + strValue + strSufix;
	return strValue;
}

function GetHInputUnformattedValue(objInput)
{
//	return (document.activeElement != objInput || objInput.readOnly)?GetUnformattedValue(objInput.value, objInput.Sufix, objInput.Prefix, objInput.Comma, objInput.Decimal, objInput.nDecimals, objInput.NumericFieldType):objInput.value;
	//alert(document.activeElement);
	//alert(objInput);
	//var strFormattedVal = (document.activeElement != objInput || objInput.readOnly) ? objInput.value : GetFormattedValue(objInput.value, objInput.Sufix, objInput.Prefix, objInput.Comma, objInput.Decimal, objInput.nDecimals, objInput.NumericFieldType);
	try
	{
		if (document.activeElement != objInput || objInput.readOnly)
		{
			var strFormattedVal = objInput.value;
		}
		else
		{
			var strFormattedVal = GetFormattedValue(objInput.value, objInput.Sufix, objInput.Prefix, objInput.Comma, objInput.Decimal, objInput.nDecimals, objInput.NumericFieldType);
		}
	}catch(exception)
	{
		var strFormattedVal = objInput.value;
	}
	return GetUnformattedValue(strFormattedVal, objInput.Sufix, objInput.Prefix, objInput.Comma, objInput.Decimal, objInput.nDecimals, objInput.NumericFieldType);
}

function GetHInputDistributionUnformattedValue(objInput)
{
	var strFormattedVal = (/*document.activeElement != objInput ||*/ objInput.readOnly) ? objInput.value : GetFormattedValue(objInput.value, objInput.Sufix, objInput.Prefix, objInput.Comma, objInput.Decimal, objInput.nDecimals, objInput.NumericFieldType);
	return GetUnformattedValue(strFormattedVal, objInput.Sufix, objInput.Prefix, objInput.Comma, objInput.Decimal, objInput.nDecimals, objInput.NumericFieldType);
}

function GetUnformattedValue(strValue,strSufix,strPrefix,strComma,strDecimal,strnDecimals,strNumericFieldType)
{
	if (strValue == "" || strValue == null)
		return strValue;

	strValue = (strSufix != "")?strValue.substring(0,strValue.length-strSufix.length):strValue;
	strValue = (strPrefix != "")?strValue.substring(strPrefix.length):strValue;

	if (strNumericFieldType.toUpperCase() == "BYTE" || strNumericFieldType.toUpperCase() == "INT16" || strNumericFieldType.toUpperCase() == "INT32" || strNumericFieldType.toUpperCase() == "DECIMAL")
	{
		if(strnDecimals == "-1")
			strnDecimals = "0";

		if(strDecimal != "")
			strValue = strValue.replace(eval("/(\\" + strDecimal + ")/g"), String.fromCharCode(2));
		if(strComma != "")
			strValue = strValue.replace(eval("/(\\" + strComma + ")/g"), "");
		strValue = strValue.replace(eval("/(" + String.fromCharCode(2) + ")/g"), ".");
	}

	return strValue;
}

function SetGridControlReadOnly(gridId, rPos, cPos, bNotEdit)
{
	var tblTempObj = GetGridObject(gridId);
	if (rPos < 0 || cPos < 1 || rPos > tblTempObj.rows.length - 1 || cPos > tblTempObj.rows(0).cells.length - 1)
		return;
	SetHControlReadOnly(tblTempObj.rows(rPos).cells(cPos).childNodes(0), bNotEdit);
}

function SetGridColumnReadOnly(gridId, colPos, bNotEdit)
{
	var tblTempObj = GetGridObject(gridId);
	if (colPos < 1 || colPos > tblTempObj.rows(0).cells.length - 1)
		return;
	for (var i=0; i<tblTempObj.rows.length; ++i)
		SetGridControlReadOnly(gridId, i, colPos, bNotEdit);
}

function SetGridLineReadOnly(gridId, rPos, bNotEdit)
{
	var tblTempObj = GetGridObject(gridId);
	if (rPos < 0 || rPos > tblTempObj.rows.length - 1)
		return;
	var j = 1;
	while (j<tblTempObj.rows(rPos).cells.length)
		SetGridControlReadOnly(gridId, rPos, j++, bNotEdit);

	var trHdr = tblTempObj.rows(rPos).cells(0);
	if (rPos > tblTempObj.rows.length - 2)
		return;
		
	trHdr.childNodes(1).style.visibility = (bNotEdit)?"hidden":"visible";
	trHdr.style.backgroundColor = (bNotEdit)?trHdr.childNodes(0).style.backgroundColor:trHdr.style.backgroundColor;
}

function SetGridReadOnly(gridId, bNotEdit)
{
	var tblTempObj = GetGridObject(gridId);
	var tblHdrObj = GetGridHeaderObject(gridId);
	var gridOuter = GetGridSpan(gridId);
	var i = 0;
	gridOuter.GridReadOnly = bNotEdit;
	
	while (i<tblTempObj.rows.length)
		SetGridLineReadOnly(gridId, i++, bNotEdit);

	tblHdrObj.rows(0).cells(0).childNodes(0).childNodes(0).style.visibility = (bNotEdit)?"hidden":"visible";
	
	if (bNotEdit)
		tblTempObj.rows(tblTempObj.rows.length-1).style.display = "none";
	else
		if (gridOuter.MaxRowsCount == null || tblTempObj.rows.length <= IntFrom(gridOuter.MaxRowsCount))
			tblTempObj.rows(tblTempObj.rows.length-1).style.display = "";
}

function SetHControlReadOnly(ctrlElem, bNotEdit)
{
	ctrlElem.ControlReadOnly = (bNotEdit)?true:false;
	ctrlElem.style.backgroundColor = (bNotEdit)?colorGrey:colorWhite;
	ctrlElem.tabIndex = (bNotEdit)?-1:0;

	if(ctrlElem.ControlType == "HInput" || ctrlElem.ControlType == "HSelect" || ctrlElem.ControlType == "HSelection")
		ctrlElem.readOnly = ctrlElem.ControlReadOnly;
	if(ctrlElem.ControlType == "HCheckBox")
		ctrlElem.disabled = ctrlElem.ControlReadOnly;
	if(ctrlElem.ControlType == "HDate")
	{
		ctrlElem.childNodes(0).readOnly = ctrlElem.ControlReadOnly;
		ctrlElem.Background = ctrlElem.style.backgroundColor
	}
}

function SetHSelectionPartialReadOnly(ctrlElem, bNotEdit, bgColor)
{
	ctrlElem.ControlPartialReadOnly = ctrlElem.readOnly = (bNotEdit != "undefined" && bNotEdit != null ? bNotEdit.toString().toUpperCase() == "TRUE" : false);
	ctrlElem.ControlReadOnly = false;

	if (bgColor != null)
		ctrlElem.style.backgroundColor = bgColor;
	else
		if (bNotEdit == true)
			ctrlElem.style.backgroundColor = colorGrey;
		else
			ctrlElem.style.backgroundColor = colorWhite;
}

function SetGridColumnPartialReadOnly(gridId, colPos, bNotEdit, bgColor)
{
	var tblTempObj = GetGridObject(gridId);
	if (colPos < 1 || colPos > tblTempObj.rows(0).cells.length - 1 || tblTempObj.rows(0).cells(colPos).childNodes(0).ControlType != "HSelection")
		return;
	for (var i=0; i<tblTempObj.rows.length; ++i)
		SetHSelectionPartialReadOnly(tblTempObj.rows(i).cells(colPos).childNodes(0), bNotEdit, bgColor);
}

function SetHControlPositive(tempObj, bPositive)
{
	switch(tempObj.ControlType)
	{
		case "HInput":
			if (bPositive)
				tempObj.Positive = true;
			else
				tempObj.Positive = null;
			break;
	}
}

function SetHControlStrictPositive(tempObj, bStrictPositive)
{
	switch(tempObj.ControlType)
	{
		case "HInput":
			if (bStrictPositive)
				tempObj.StrictPositive = true;
			else
				tempObj.StrictPositive = null;
			break;
	}
}

function SetHControlNegative(tempObj, bNegative)
{
	switch(tempObj.ControlType)
	{
		case "HInput":
			if (bNegative)
				tempObj.Negative = true;
			else
				tempObj.Negative = null;
			break;
	}
}

function SetHControlStrictNegative(tempObj, bStrictNegative)
{
	switch(tempObj.ControlType)
	{
		case "HInput":
			if (bStrictNegative)
				tempObj.StrictNegative = true;
			else
				tempObj.StrictNegative = null;
			break;
	}
}

function SetGridColumnPositive(gridId, colPos, bPositive)
{
	var tblTempObj = document.all(gridId + "BigSpan").childNodes(1).childNodes(0);
	if (colPos < 1 || colPos > tblTempObj.rows(0).cells.length - 1 || tblTempObj.rows(0).cells(colPos).childNodes(0).ControlType != "HInput")
		return;
	for (var i=0; i<tblTempObj.rows.length; ++i)
		SetHControlPositive(tblTempObj.rows(i).cells(colPos).childNodes(0), bPositive);
}

function SetGridColumnStrictPositive(gridId, colPos, bStrictPositive)
{
	var tblTempObj = document.all(gridId + "BigSpan").childNodes(1).childNodes(0);
	if (colPos < 1 || colPos > tblTempObj.rows(0).cells.length - 1 || tblTempObj.rows(0).cells(colPos).childNodes(0).ControlType != "HInput")
		return;
	for (var i=0; i<tblTempObj.rows.length; ++i)
		SetHControlStrictPositive(tblTempObj.rows(i).cells(colPos).childNodes(0), bStrictPositive);
}

function SetGridColumnNegative(gridId, colPos, bNegative)
{
	var tblTempObj = document.all(gridId + "BigSpan").childNodes(1).childNodes(0);
	if (colPos < 1 || colPos > tblTempObj.rows(0).cells.length - 1 || tblTempObj.rows(0).cells(colPos).childNodes(0).ControlType != "HInput")
		return;
	for (var i=0; i<tblTempObj.rows.length; ++i)
		SetHControlNegative(tblTempObj.rows(i).cells(colPos).childNodes(0), bNegative);
}

function SetGridColumnStrictNegative(gridId, colPos, bStrictNegative)
{
	var tblTempObj = document.all(gridId + "BigSpan").childNodes(1).childNodes(0);
	if (colPos < 1 || colPos > tblTempObj.rows(0).cells.length - 1 || tblTempObj.rows(0).cells(colPos).childNodes(0).ControlType != "HInput")
		return;
	for (var i=0; i<tblTempObj.rows.length; ++i)
		SetHControlStrictNegative(tblTempObj.rows(i).cells(colPos).childNodes(0), bStrictNegative);
}

function SetGridColumnVisible(gridId, colPos, bShow, iWidth, strName)
{
	var tblTempObj = GetGridObject(gridId);

	if (colPos < 1 || colPos > tblTempObj.rows(0).cells.length - 1)
		return;

	var tblHdrObj = GetGridHeaderObject(gridId);

	for (var i=0; i<tblTempObj.rows.length; ++i)
		if (bShow)
		{
			SetCellVisible(tblTempObj.rows(i).cells(colPos), bShow);
			SetCellVisible(tblHdrObj.rows(0).cells(colPos), bShow);
			if (iWidth == null)
			{
				if (tblTempObj.rows(i).cells(colPos).childNodes(0).TempWidth != null)
					tblTempObj.rows(i).cells(colPos).childNodes(0).style.width = tblTempObj.rows(i).cells(colPos).childNodes(0).TempWidth;
			}
			else
			{
				if (tblTempObj.rows(i).cells(colPos).childNodes(0).ControlType == "HDate")
					iWidth = 86;

				tblTempObj.rows(i).cells(colPos).childNodes(0).style.width = tblHdrObj.rows(0).cells(colPos).childNodes(0).style.width = iWidth;
			}
			if (strName != null)
				tblHdrObj.rows(0).cells(colPos).childNodes(0).innerText = strName;
		}
		else
		{
			SetCellVisible(tblTempObj.rows(i).cells(colPos), bShow);
			SetCellVisible(tblHdrObj.rows(0).cells(colPos));
		}
}

function SetCellVisible(ctrlElem, bShow)
{
	ctrlElem.childNodes(0).style.display = (bShow)?"":"none";
	ctrlElem.style.border = (bShow)?"gray 1px solid":"0";
	if(bShow != true)
	{
		ctrlElem.childNodes(0).TempWidth = ctrlElem.childNodes(0).style.width;
		ctrlElem.childNodes(0).style.width = 0;
	}
}

function SetHeaderVisible(trHdr, bVisible)
{
	trHdr.childNodes(0).style.width = (!bVisible)?35:25;
	trHdr.style.backgroundColor = trHdr.childNodes(0).style.backgroundColor;
	trHdr.childNodes(1).style.display = (!bVisible)?"none":"";
}

function SetGridFooterVisible(gridId, bShow)
{
	if (GetGridSpan(gridId) == null)
		return;
	var tblTempObj = GetGridObject(gridId);
	tblTempObj.rows(tblTempObj.rows.length - 1).style.display = (bShow)?"":"none";
//	document.all(gridId + "ShowFooter").value = bShow.toString();
}

function IsGridFooterVisible(gridId)
{
	var tblTempObj = GetGridObject(gridId);
	return tblTempObj.rows(tblTempObj.rows.length - 1).style.display == "";
}

function SetGridControlValue(gridId, rPos, cPos, strValue)
{
	var tblTempObj = GetGridObject(gridId);
	if (rPos < 0 || cPos < 1 || rPos > tblTempObj.rows.length - 1 || cPos > tblTempObj.rows(0).cells.length - 1)
		return;
	SetHControlValue(tblTempObj.rows(rPos).cells(cPos).childNodes(0), strValue);
}

function SetDirtyGrid(gridId)
{
	if (document.all(gridId + "ChangeFlag") != null)
		document.all(gridId + "ChangeFlag").value = "changed";
}

function GeneralBoolValue(bValue, bType)
{
	if((bValue == false || bValue == "false" || bValue == "FALSE" || bValue == "False" || bValue == 0 || bValue == "0") && !bType)
		return true;
	if((bValue == true || bValue == "true" || bValue == "TRUE" || bValue == "True" || bValue == 1 || bValue == "1") && bType)
		return true;
	
	return false;
}

function SetHControlValue(ctrlElem, strValue, bDefault, bSetDirty)
{
	if(bSetDirty != false)
		bSetDirty = true;

	if(ctrlElem.ControlType == "HDate")
	{
		if (ctrlElem.childNodes(0).value != strValue)
		{
			SetHDateValue(ctrlElem.childNodes(0), strValue, ctrlElem.childNodes(0).Pattern);
			if (ctrlElem.GridControl)
				SetDirtyGrid(ctrlElem.GridId);
		}
		if(bDefault)
			ctrlElem.DefaultValue = strValue;
	}
	if(ctrlElem.ControlType == "HInput")
	{
		try
		{
				if (document.activeElement != ctrlElem)
				{
					var tempValue = GetFormattedValue(strValue, ctrlElem.Sufix, ctrlElem.Prefix, ctrlElem.Comma, ctrlElem.Decimal, ctrlElem.nDecimals, ctrlElem.NumericFieldType);
					if (ctrlElem.value != tempValue)
					{
						ctrlElem.value = tempValue;
						ctrlElem.title = (ctrlElem.ToolTip != null)?ctrlElem.value:null;
						if (ctrlElem.GridControl)
						{
							ctrlElem.InitialValue = strValue;
							SetDirtyGrid(ctrlElem.GridId);
						}
					}
				}
				else
				{
					if (ctrlElem.value != strValue)
					{
						ctrlElem.value = strValue;
						ctrlElem.title = (ctrlElem.title != null)?ctrlElem.value:null;
					}
				}
		} catch(e)
		{
					if (ctrlElem.value != strValue)
					{
						ctrlElem.value = strValue;
						ctrlElem.title = (ctrlElem.title != null)?ctrlElem.value:null;
					}
		}
		if(bDefault)
			ctrlElem.DefaultValue = strValue;
	}
	if(ctrlElem.ControlType == "HSelect")
	{
		var tempSel = ctrlElem.parentElement.childNodes(1);
		var k = 0;
		while (ctrlElem.CodeValue != strValue && k<tempSel.options.length)
		{
			if (tempSel.options[k].value == strValue)
			{
				selIndex = k;
				ctrlElem.value = tempSel.options[k].text;
				ctrlElem.title = (ctrlElem.ToolTip == "true")?tempSel.options[k].text:null;
				ctrlElem.CodeValue = tempSel.options[k].value;
				tempSel.options[k].selected = true;
				ctrlElem.InitialSelectedIndex = k;
				k = tempSel.options.length;
				if(bSetDirty)
					SetDirtyGrid(ctrlElem.GridId);
			}
			k++;
		}	
		if(bDefault)
		{				
			var tempSel = ctrlElem.parentElement.childNodes(1);
			if (tempSel.options.length > 0)
			{
				if (tempSel.options[0].value != "" && strValue == "")
				{
					ctrlElem.DefaultValue = null;
				}
				else
					for (var k=0; k<tempSel.options.length; ++k)
					{
						if (tempSel.options[k].value == strValue)
						{
							ctrlElem.DefaultValue = tempSel.options[k].value;
							k = tempSel.options.length;
						}
					}
			}
		}
	}
	if(ctrlElem.ControlType == "HSelection")
	{
		if (ctrlElem.value != strValue)
		{
			ctrlElem.InitialValue = ctrlElem.value = strValue;
			ctrlElem.title = (ctrlElem.title != null)?strValue:null;
			SetDirtyGrid(ctrlElem.GridId);
		}
		if(bDefault)
			ctrlElem.DefaultValue = strValue;
		if(bSetDirty)
			SetDirtyGrid(ctrlElem.GridId);
	}
	if(ctrlElem.ControlType == "HCheckBox")
	{
		if (ctrlElem.checked)
		{
			if (GeneralBoolValue(strValue, false))
				ctrlElem.checked = false;
		}
		else
		{
			if (GeneralBoolValue(strValue, true))
				ctrlElem.checked = true;
		}

		if (ctrlElem.GridControl && (GeneralBoolValue(strValue, true) && !ctrlElem.checked || GeneralBoolValue(strValue, false)))
			SetDirtyGrid(ctrlElem.GridId);

		if(bDefault)
			if (GeneralBoolValue(strValue, true))
			{
				ctrlElem.checked = true;
				ctrlElem.DefaultValue = strValue;
			}
			if (GeneralBoolValue(strValue, false))
			{
				ctrlElem.checked = false;
				ctrlElem.removeAttribute("DefaultValue");
			}
	}
}

function SetHControlDefaultValue(ctrlElem, strValue)
{
	SetHControlValue(ctrlElem, strValue, true);
}

function SetDeleteAllLinesButtonVisible(gridId, bShow)
{
	var tblTempObj = GetGridObject(gridId);
	var tblHdrObj = GetGridHeaderObject(gridId);

	tblHdrObj.rows(0).cells(0).childNodes(0).childNodes(0).style.visibility = (bShow)?"visible":"hidden";
}

function SetDeleteLineButtonsVisible(gridId, arrIndexes, bShow)
{
	var tblTempObj = GetGridObject(gridId);

	if (arrIndexes.length == 1 && arrIndexes[0] == -1)
		for (var i=0; i<tblTempObj.rows.length-1; ++i)
			SetHeaderVisible(tblTempObj.rows(i).cells(0), bShow)
	else
		for (var i=0; i<arrIndexes.length; ++i)
			SetHeaderVisible(tblTempObj.rows(arrIndexes[i]).cells(0), bShow)
}

function AddGridLine(gridId, arrValues)
{
	var tblTempObj = GetGridObject(gridId);
	InsertBlankRow(tblTempObj);
	var trElement = tblTempObj.rows(tblTempObj.rows.length - 2);
	var j = 1;
	while (j<trElement.cells.length)
	{
		SetGridControlValue(gridId, tblTempObj.rows.length - 2, j, arrValues[j-1]);
		j++;
	}

	SetDirtyGrid(gridId);
	AddLine(trElement);
}

function DeleteGridLine(gridId, lineIndex)
{
	GetGridObject(gridId).rows(lineIndex).cells(0).childNodes(1).click();
}

function DeleteAllGridLines(gridId)
{
	bAlternativeDeleteString = false;
	GetGridHeaderObject(gridId).rows(0).cells(0).childNodes(0).childNodes(0).click();
	bAlternativeDeleteString = true;
}

function OnFirstPage(gridId)
{
	var inpStatus = SaveSendGrid(gridId);
	
	event.returnValue = false;
	var nPageIndex = IntFrom(document.all(gridId +"Paging").PageIndex);
	if (nPageIndex == 0)
		return;
	else
		document.all(gridId +"Paging").PageIndex = 0;

	GenerateGrid(gridId, true);
	if (document.all(gridId + "InvokeOnRefreshGrid") != null)
		window.execScript(document.all(gridId + "InvokeOnRefreshGrid").value, "JavaScript");
}

function OnPreviousPage(gridId)
{
	var inpStatus = SaveSendGrid(gridId);

	event.returnValue = false;
	var nPageIndex = IntFrom(document.all(gridId +"Paging").PageIndex);
	if (nPageIndex == 0)
		return;
	else
		document.all(gridId +"Paging").PageIndex = nPageIndex - 1;

	GenerateGrid(gridId, true);
	if (document.all(gridId + "InvokeOnRefreshGrid") != null)
		window.execScript(document.all(gridId + "InvokeOnRefreshGrid").value, "JavaScript");
}

function OnNextPage(gridId)
{
var arrMatrix = arrayGridSource;

	var inpStatus = SaveSendGrid(gridId);

	event.returnValue = false;
	var nPageIndex = IntFrom(document.all(gridId + "Paging").PageIndex);
	var nPageSize = IntFrom(document.all(gridId + "Paging").PageSize);

	var iPos = IntFrom(inpStatus.index);
	var gridValues = new Array();
	gridValues = arrMatrix[iPos];

	if (nPageSize*(nPageIndex+1) >= gridValues.length)
		return;
	else
		document.all(gridId +"Paging").PageIndex = nPageIndex + 1;

	GenerateGrid(gridId, true);
	if (document.all(gridId + "InvokeOnRefreshGrid") != null)
		window.execScript(document.all(gridId + "InvokeOnRefreshGrid").value, "JavaScript");
}

function OnLastPage(gridId)
{
var arrMatrix = arrayGridSource;

	var inpStatus = SaveSendGrid(gridId);

	event.returnValue = false;
	var nPageIndex = IntFrom(document.all(gridId +"Paging").PageIndex);
	var nPageSize = IntFrom(document.all(gridId +"Paging").PageSize);

	var iPos = IntFrom(inpStatus.index);
	var gridValues = new Array();
	gridValues = arrMatrix[iPos];

	if (nPageSize*nPageIndex >= gridValues.length)
		return;
	else
		document.all(gridId +"Paging").PageIndex = Math.ceil(gridValues.length/nPageSize) - 1;

	GenerateGrid(gridId, true);
	if (document.all(gridId + "InvokeOnRefreshGrid") != null)
		window.execScript(document.all(gridId + "InvokeOnRefreshGrid").value, "JavaScript");
}

function OnPageInputKeyDown(oTmp, keyCode)
{
var arrMatrix = arrayGridSource;

	if (keyCode != 13)
		return;

	var val = oTmp.value;
	val = val.replace(/(^\s*)|(\s*$)/g, "");

	while (val.indexOf('0') == 0)
		val = val.substr(1);
	if (val.length > 0 && val.substr(0, 1) == '.')
		val = "0" + val;

	var inpStatus = document.all(oTmp.GridId + "ChangeFlag");
	var iPos = IntFrom(inpStatus.index);
	var gridValues = new Array();
	gridValues = arrMatrix[iPos];
	var nPageSize = IntFrom(document.all(oTmp.GridId +"Paging").PageSize);

	if (IntFrom(val).toString() == val && Math.ceil(gridValues.length/nPageSize) >= val && val >=0)
	{
		oTmp.InitialValue = val - 1;
		document.all(oTmp.GridId + "Paging").PageIndex = val - 1;
		GenerateGrid(oTmp.GridId, true);
		if (document.all(oTmp.GridId + "InvokeOnRefreshGrid") != null)
			window.execScript(document.all(oTmp.GridId + "InvokeOnRefreshGrid").value, "JavaScript");
	}

	oTmp.value = oTmp.InitialValue + 1;
}

function OnPageInputBeforeDeactivate(oTmp)
{
	if (oTmp.value != (IntFrom(oTmp.InitialValue) + 1).toString())
		OnPageInputKeyDown(oTmp, 13);
}

function OnPageSizeInputKeyDown(oTmp, keyCode)
{
var arrMatrix = arrayGridSource;

	if (keyCode != 13)
		return;

	var val = oTmp.value;
	val = val.replace(/(^\s*)|(\s*$)/g, "");

	while (val.indexOf('0') == 0)
		val = val.substr(1);
	if (val.length > 0 && val.substr(0, 1) == '.')
		val = "0" + val;

	var inpStatus = document.all(oTmp.GridId + "ChangeFlag");
	var iPos = IntFrom(inpStatus.index);
	var gridValues = new Array();
	gridValues = arrMatrix[iPos];

	if (IntFrom(val).toString() == val && val <= 50 && val >=0)
	{
		oTmp.InitialValue = val;
		document.all(oTmp.GridId + "Paging").PageIndex = 0;
		document.all(oTmp.GridId + "Paging").PageSize = val;
		GenerateGrid(oTmp.GridId, true);
		if (document.all(oTmp.GridId + "InvokeOnRefreshGrid") != null)
			window.execScript(document.all(oTmp.GridId + "InvokeOnRefreshGrid").value, "JavaScript");
	}

	oTmp.value = oTmp.InitialValue;
}

function OnPageSizeInputBeforeDeactivate(oTmp)
{
	if (oTmp.value != oTmp.InitialValue)
		OnPageSizeInputKeyDown(oTmp, 13);
}

function OnFilterGrid(eObj, arColumns, arSearchColumns)
{
	var oTmp = document.all(eObj.GridId + "FilterGrid");

	if (oTmp.value != oTmp.InitialValue)
		OnFilterGridInputKeyDown(oTmp, 13, arColumns, arSearchColumns);

	event.returnValue = false;
}

function OnFilterGridInputKeyDown(oTmp, keyCode, arColumns, arSearchColumns)
{
	if (keyCode != 13)
		return;

	var gridId = oTmp.GridId;
	var inpStatus = SaveSendGrid(gridId);

	FilterGrid(gridId, arColumns, arSearchColumns);

}

function GetColumnsForGrid(gridId)
{
	var arColumns = eval(document.all[gridId + "ColumnNames"].value);
	return arColumns;
}

function ForceFilterGrid(gridId)
{
	if (document.all[gridId + "FilterGrid"].parentElement.parentElement.cells.length >= 9)
	{
		var s = document.all[gridId + "FilterGrid"].parentElement.parentElement.cells[9].childNodes(0).onclick.toString().split("{")[1].split("}")[0].substr(3).replace("this", "'" + gridId + "'");
		eval(s);
	}
}

function FilterGrid(gridId, arColumns, arSearchColumns)
{
	var oTmp = document.all(gridId + "FilterGrid");

	var strFilter = "";
	var s = oTmp.value.toLowerCase();
	var oGrid = GetGridObject(gridId);
	var objRowFooter = oGrid.rows[oGrid.rows.length-1];

	if (s != "")
	{
		for (var j=0; j<objRowFooter.cells.length-1; ++j)
		{
			s = oTmp.value.toLowerCase();
			if (arSearchColumns[j].toUpperCase() == "TRUE")
			{
				var hObj = objRowFooter.cells[j+1].childNodes(0);

				if(hObj.ControlType == "HInput")
				{
					var s1 = s;
					if ((hObj.NumericFieldType.toUpperCase() == "DECIMAL" && IsDecimal(s1)) || 
						(hObj.NumericFieldType.toUpperCase() == "INT32" && IsInt32(s1)) || 
						(hObj.NumericFieldType.toUpperCase() == "INT16" && IsInt16(s1)) || 
						(hObj.NumericFieldType.toUpperCase() == "BYTE" && IsByte(s1)))
					{
						strFilter += "[" + arColumns[j] + "] = " + s1 + " OR ";
					}
					else
						if (hObj.NumericFieldType.toUpperCase() == "STRING")
							strFilter += "[" + arColumns[j] + "] LIKE '%" + s + "%' OR ";
				}
				if(hObj.ControlType == "HSelection")
					strFilter += "[" + arColumns[j] + "] LIKE '%" + s + "%' OR ";
				if(hObj.ControlType == "HSelect")
				{
					var a = new Array();
					var objSelect = objRowFooter.cells[j+1].childNodes(1);

					for (var k=0; k<objSelect.options.length; ++k)
						if (StringPos(objSelect.options[k].text, s) != -1)
							a.push(objSelect.options[k].value);
					var v = GetParamArray(a, "'");
					if (v != "")
						strFilter += "Convert([" + arColumns[j] + "], 'System.String') IN (" + v + ") OR ";
				}
			}
		}
	}

	// pastrez si row-urile nou adaugate
	var bColIDexists = false;
	for (var i=0; i<arColumns.length; ++i)
		if (arColumns[i] == "ID")
			bColIDexists = true;
	if (bColIDexists)
		strFilter = (strFilter.length > 0) ? strFilter + "[ID] &lt; 0 OR " : strFilter;

	strFilter = (strFilter.length > 0) ? strFilter.substring(0, strFilter.length - 4) : strFilter;
	var strGridFilter = document.all[gridId + "FilterString"].value;
	strFilter = (strGridFilter.length > 0) ? "(" + strGridFilter + (strFilter.length > 0 ? ") AND (" : "") + strFilter + ")" : strFilter;

	oTmp.InitialValue = oTmp.value;

	if (s.replace(/(\?+)/g, "") != "" || s == "")
		RefreshGrid(strFilter, arColumns, gridId);
}

function GetParamArray(a, strSep)
{
	if (strSep == null)
		strSep = "";
	var s = "";
	for (var i=0; i<a.length; ++i)
	{
		s += strSep + a[i] + strSep;
		if (i < a.length-1)
			s += ", ";
	}
	return s;
}

function StringPos(s, x)
{
	var xx = "(";
	xx += x;
	xx += ")";
	var re = new RegExp(xx, "ig");
	var z = s.search(re);
	return z;
}

function TrimStart(val, c)
{
	if (val == "0")
		return val;
	while (val.indexOf(c) == 0)
		val = val.substr(1);
	if (val.length > 0 && val.substr(0, 1) == '.')
		val = "0" + val;
	return val;
}

function TrimEnd(val, c)
{
	if (val.indexOf('.') == -1)
		return val;
	while (val.lastIndexOf(c) == val.length-1)
		val = val.substr(0, val.length-1);
	if (val.lastIndexOf('.') == val.length-1)
		val = val.substr(0, val.length-1);
	return val;
}

function TrimSpaces(val)
{
    return val.replace(/(^\s*)|(\s*$)/g, "");
}

function IsInt32(val)
{
	val = TrimSpaces(val);
	val = TrimStart(val, '0');
	return (val == '' || IntFrom(val).toString() == val) && parseFloat(val) >= -2147483648 && parseFloat(val) <= 2147483467;
}

function IsInt16(val)
{
	val = TrimSpaces(val);
	val = TrimStart(val, '0');
	return (val == '' || IntFrom(val).toString() == val) && IntFrom(val) >= -32768 && IntFrom(val) <= 32767;
}

function IsByte(val)
{
	val = TrimSpaces(val);
	val = TrimStart(val, '0');
	return (val == '' || IntFrom(val).toString() == val) && IntFrom(val) >= 0 && IntFrom(val) <= 255;
}

function IsDecimal(val)
{
	val = TrimSpaces(val);
	val = TrimStart(val, '0');
	val = TrimEnd(val, '0');
	return (val == '' || parseFloat(val).toString() == val) && (parseFloat(val) + Math.pow(10,28) + 1 >= 0) && (parseFloat(val) - Math.pow(10,28) + 1 <= 0);
}

function DisplayGridRow(gridId, rPos)
{
	var nPageSize = IntFrom(document.all(gridId +"Paging").PageSize);
	var nPageIndex = Math.floor(rPos/nPageSize);
	var nOldPageIndex = IntFrom(document.all(gridId +"Paging").PageIndex);

	if (nPageIndex != nOldPageIndex)
	{
		document.all(gridId +"Paging").PageIndex = nPageIndex;
		GenerateGrid(gridId, true);
	}
}

function VerifyIfUnicity(oTmp)
{
var arrMatrix = arrayGridSource;

	var strGridID = oTmp.GridId;
	var rowHeader = document.all(strGridID + "HeaderSpan").childNodes(0).rows(0);
	var nIndex = oTmp.parentElement.cellIndex - 1;

	if (rowHeader.cells(nIndex+1).childNodes(0).UniqueValue == "true")
	{
		var inpStatus = document.all(strGridID + "ChangeFlag");
		var iPos = IntFrom(inpStatus.index);
		var gridValues = new Array();
		gridValues = arrMatrix[iPos];

		var nPageSize = 0;
		var nPageIndex = 0;
		if (document.all(strGridID +"Paging") != null)
		{
			nPageSize = IntFrom(document.all(strGridID +"Paging").PageSize);
			nPageIndex = IntFrom(document.all(strGridID +"Paging").PageIndex);
		}
		var rIndex = oTmp.parentElement.parentElement.rowIndex + nPageSize*nPageIndex;

		var bUnique = true;
		for (var j=0; j<gridValues.length; ++j)
		{
			if (j != rIndex)
			{
				switch (oTmp.ControlType)
				{
					case "HSelection":
					case "HInput":
						if (oTmp.value.toUpperCase() == gridValues[j][nIndex].toUpperCase())
						{
							bUnique = false;
							j = gridValues.length;
						}
						break;
					case "HSelect":
						if (oTmp.parentElement.childNodes(1).options[oTmp.parentElement.childNodes(1).selectedIndex].value == gridValues[j][nIndex])
						{
							bUnique = false;
							j = gridValues.length;
						}
						break;
					case "HCheckBox":
						if (oTmp.checked && gridValues[j][nIndex].toString().toUpperCase() == "TRUE")
						{
							bUnique = false;
							j = gridValues.length;
						}
						break;
				}
			}
		}
		return bUnique;
	}

	return true;
}

function VerifyUniqueValue(strGridID, nIndex, strControlType, strValue)
{
var arrMatrix = arrayGridSource;

	var rowHeader = document.all(strGridID + "HeaderSpan").childNodes(0).rows(0);
	if (rowHeader.cells(nIndex+1).childNodes(0).UniqueValue == "true")
	{
		var inpStatus = document.all(strGridID + "ChangeFlag");
		var iPos = IntFrom(inpStatus.index);
		var gridValues = new Array();
		gridValues = arrMatrix[iPos];

		var bUnique = true;
		for (var j=0; j<gridValues.length; ++j)
		{
			if (j != nIndex)
			{
				if (strValue == gridValues[j][nIndex])
				{
					bUnique = false;
					j = gridValues.length;
				}
			}
		}
		return bUnique;
	}

	return true;
}

function SetMaxRowsCount(gridId, nRowCount)
{
	var gridOuter = GetGridSpan(gridId);
	gridOuter.MaxRowsCount = nRowCount;
}

function SelectControlHeaders(oTmp)
{
	var editSelHeaderColor;
	var editHeaderColor;

	if (oTmp != null)
	{
		editSelHeaderColor = document.all(oTmp.GridId + "HRSBGColor").value;
		editHeaderColor = document.all(oTmp.GridId + "HRBGColor").value;
	}
	else
		if (currentHGridSel != null)
		{
			editSelHeaderColor = document.all(currentHGridSel.GridId + "HRSBGColor").value;
			editHeaderColor = document.all(currentHGridSel.GridId + "HRBGColor").value;
		}
		else
			return;

	if (currentHGridSel != null)
	{
		currentHGridSel.parentElement.parentElement.childNodes(0).style.backgroundColor = editHeaderColor;
		currentHGridSel.parentElement.parentElement.childNodes(0).childNodes(0).style.backgroundColor = editHeaderColor;
		currentHGridSel.parentElement.parentElement.childNodes(0).childNodes(1).style.backgroundColor = editHeaderColor;
		currentHGridSel.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.childNodes(0).childNodes(0).rows(0).childNodes(currentHGridSel.parentElement.cellIndex).style.backgroundColor = editHeaderColor;
	}

	if (oTmp != null)
	{
		oTmp.parentElement.parentElement.childNodes(0).style.backgroundColor = editSelHeaderColor;
		oTmp.parentElement.parentElement.childNodes(0).childNodes(0).style.backgroundColor = editSelHeaderColor;
		oTmp.parentElement.parentElement.childNodes(0).childNodes(1).style.backgroundColor = editSelHeaderColor;
		oTmp.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.childNodes(0).childNodes(0).rows(0).childNodes(oTmp.parentElement.cellIndex).style.backgroundColor = editSelHeaderColor;
	}

	currentHGridSel = oTmp;
}

function ForceSendChanges(gridId, bForce)
{
	if (bForce == null)
		bForce = false;
	if (bForce)
		document.all(gridId + "ChangeFlag").value = "changed";
	SendChanges(gridId);
}

function DoCallWebService_Grid(strFuncName, arStrParams)
{
	var headObj = new Object();
	var callObj = service.createCallOptions();
	callObj.async = false;
	callObj.params = new Array();
	callObj.params.arStrParams = arStrParams;
	callObj.params.strMethodName = strFuncName;
	callObj.funcName = "DoCallWebService_Grid";
//	callObj.funcName = strFuncName;
	callObj.SOAPHeader = new Array();
	callObj.SOAPHeader[0] = headObj;
	var iCallID = service["HGridOperations"].callService(callObj);

	var strError = "";
	if (iCallID == null || iCallID.value == null || iCallID.error)
		strError = iCallID.errorDetail.code + " | " + iCallID.errorDetail.string + " | " + iCallID.errorDetail.raw;
	else
		if (iCallID.value[0] == "1")
			strError = iCallID.value[1];
	if (strError != "")
	{
		alert(strError);
		if (event)
			event.returnValue = false;
		return null;
	}
	return iCallID.value.slice(1);
}

function DoUnpack(strValues, strRowSep, strCellSep)
{
	if (strValues == "")
		return new Array();

	if (strRowSep == null)
		strRowSep = rowSep;
	if (strCellSep == null)
		strCellSep = cellSep;

	var arTemp = new Array();
	var val = strValues.split(strRowSep);

	for (var i=0; i<val.length; ++i)
		arTemp[i] = val[i].split(strCellSep);

	return arTemp;
}

function DoPack(ar, strRowSep, strCellSep)
{
	if (strRowSep == null)
		strRowSep = rowSep;
	if (strCellSep == null)
		strCellSep = cellSep;

	var a = new Array();
	for (var i=0; i<ar.length; ++i)
		a[i] = ar[i].join(strCellSep);
	return a.join(strRowSep);
}

function ReplaceRowCellSeparators(val)
{
	return val.replace(/(\x02\x04)/g, "%02%04").replace(/(\x03\x04)/g, "%03%04");
}

function MarkRowAsChanged(gridId, rPos)
{
var arrMatrix = arrayGridSource;
var arrNewValues = arrayGridChanges;

	var inpStatus = document.all(gridId + "ChangeFlag");
	var iPos = IntFrom(inpStatus.index);
	var gridOuter = GetGridSpan(gridId);
	var oGrid = gridOuter.childNodes(1).childNodes(0);
	var gridValues = new Array();
	gridValues = arrMatrix[iPos];
	var arrNewLine = new Array();
	arrNewLine = arrNewValues[iPos];

	var matrix = new Array();
	var arr = new Array();
	for (var s=0; s<gridValues[rPos].length; ++s)
		arr[s] = gridValues[rPos][s];
	arr[arr.length] = rPos;
	matrix[matrix.length] = arr;

	for (var i=0; i<matrix.length; ++i)
	{
		var bIndexFound = false;
		for (var j=0; j<arrNewLine.length; ++j)
		{
			if (matrix[i][matrix[i].length-1] == arrNewLine[j][arrNewLine[j].length-1])
			{
				arrNewLine[j] = matrix[i];
				j = arrNewLine.length;
				bIndexFound = true;
			}
		}
		if (!bIndexFound)
			arrNewLine[arrNewLine.length] = matrix[i];
	}

	arrNewValues[iPos] = arrNewLine;

	inpStatus.value = "changed";
}

function SetHSelectValue(oTmp, ctrlSelect, k)
{
	oTmp.CodeValue = ctrlSelect.options[k].value;
	oTmp.value = ctrlSelect.options[k].text;
	ctrlSelect.options[k].selected = true;
}

function ShowHideHSelect(oTmp, ctrlSelect)
{
	if (ctrlSelect.style.display == "")
	{
		ctrlSelect.style.display = "none";
		oTmp.style.display = "";
	}
}

function IntFrom(x)
{
	return parseInt(x, 10);
}

function IsCtrRO(oTmp)
{
	return (oTmp.ControlReadOnly == true || oTmp.ControlReadOnly == "true");
}

function IsSameValue(val, s, ii, jj, x)
{
	var s2 = val[ii][jj];
	if (x == 1)
		s2 = s2.toString();
	if (x == 2)
		s2 = s2.toString().toUpperCase();
	if (s2 != s)
	{
		val[ii][jj] = s;
		return true;
	}
	else
		return false;
}

function FocusIsInGrid(oTmp)
{
	return (event != null && event.toElement != null && event.toElement.id == "SpanContentGrid" + oTmp.GridId ||	IsCtrRO(oTmp));
}

function FocusIsInGrid2()
{
	return (event.srcElement.GridId == null && currentHGridElem != null && "SpanContentGrid" + currentHGridElem.GridId == event.srcElement.id);
}

function GetGridIdentity(strGridId)
{
	var xParams = new Array();
	xParams[0] = strGridId;
	var xResult = DoCallWebService_Grid("GetIdentity_NEW", xParams);
	if (xResult != null)
		return xResult[0];
	return -1;
}

function HasChildren(codeSource)
{
	var arrSource = arrayGridSource;
	for(var i = 0; i < arrSource.length; i++)
	{
		for(var j = 0; j < arrSource[i].length; j++)
		{
			if(arrSource[i][j][1] == codeSource)
				return true;
		}
	}
	return false;
}